Solved

how to read byte

Posted on 2006-11-04
7
362 Views
Last Modified: 2010-04-16
what functions should I use to read byte by byte from binary files? I looked up and found that the read() functions. but it returns the whole files in binary.  But I like to read byte by byte.  Thanks in advance.

Kelvin
0
Comment
Question by:templeavenue
  • 4
  • 3
7 Comments
 
LVL 17

Expert Comment

by:ramrom
ID: 17873774
From the Python Library Reference under 2.3.9 File Objects:
"read( [size])
Read at most size bytes from the file "

Nuf said?

But before you jump, are you clear on what you want do accomplish. Reading a file byte by byte is probably less efficient than reading, say, a line then accessing each byte in the line.

So what is your goal?
0
 

Author Comment

by:templeavenue
ID: 17877961
I want to make comparions byte(possiblly bits) between the byte read from the file and the reference byte(which I don't need to read from any file).  
0
 
LVL 17

Expert Comment

by:ramrom
ID: 17878378
OK. Well I think you have the information you need. Any questions?
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:templeavenue
ID: 17878426
oh...i still haven't figure out how to read byte from file.  Can you please explain?
0
 
LVL 17

Accepted Solution

by:
ramrom earned 50 total points
ID: 17878935
f = file("c:/foo.dat", "rb") # your file may have a different name
while 1:
  byte = f.read(1) # reads(per the documentation above) 1 byte
  if not byte: break # we've reached end of file
  # process the byte

0
 
LVL 17

Expert Comment

by:ramrom
ID: 17882427
I'm glad I could help. I am confused by getting a grade of C. Please note the EE guidelines for grading:

    *  A: The Expert(s) either provided you with a thorough answer or they provided you with a link to information that thoroughly answered your question. An "A" can also be given to any answer that you found informative or enlightening beyond the direct question that you asked.

    * B: The Expert(s) provided an acceptable solution, or a link to an acceptable solution, that you were able to use, although you may have needed a bit more information to complete the task.

    * C: Because Experts' reliability are often judged by their grading records, many Experts would like the opportunity to clarify if you have questions about their solutions. If you have given the Expert(s) ample time to respond to your clarification posts and you have responded to each of their posts providing requested information; or if the answers, after clarification, lack finality or do not completely address the issue presented, then a "C" grade is an option. You also have the option here of just asking Community Support to delete the question.

Basedd on that I'd have given a grade of A. Please help me understand how you came up with a different result.

I am also confused in that your original question was "what functions should I use to read byte by byte" and I answered that in my first comment.
0
 

Author Comment

by:templeavenue
ID: 17890273
well..I think I agree with you. When I graded that I just think "whatever".  Sorry, based on the questions I asked, I should have graded as A.  I am willing to change the grade, but I don't know how.. Again. sorry...you have been helped... and thanks
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Updating urls.py doesn't work 4 81
python/mysql  table update problem 10 78
Installing Flask app on IIS7 10 266
Python 2.7 - Save to file 4 36
A set of related code is known to be a Module, it helps us to organize our code logically which is much easier for us to understand and use it. Module is an object with arbitrarily named attributes which can be used in binding and referencing. …
Sequence is something that used to store data in it in very simple words. Let us just create a list first. To create a list first of all we need to give a name to our list which I have taken as “COURSE” followed by equals sign and finally enclosed …
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now