On Tue, Nov 06, 2007 at 04:20:05PM +0000, Richard Lewis wrote:
At Tue, 6 Nov 2007 15:54:35 +0000, Chris G wrote:
Are there any Python people here?
Yes, loads!
How do I do the equivalent of the C below in Python :-
while ( (str = read(file)) != NULL) { do something; }
Err, my C isn't that hot, but how do this do anything other than put all the contents of the file in str? Or does it read a line at a time in some non-obvious way?
OK, the code wasn't perfect, there need to be more parameters to read() so that it only reads a bit of the file at a time.
I can read the data OK, i.e. I have opened the file and I can successfully read from the file with the line:-
tagNum = tlvFile.read(3)
but I don't see how to make that part of a for/while loop that exits at the end of the file. I can't use an iterator because the file is read in variable length chunks whose length depends on context.
You should have a look at:
http://docs.python.org/lib/bltin-file-objects.html
If you /really/ want to read in chunks of three bytes, you could do this:
chunk = tlvFile.read(3) while chunk != "": print chunk chunk = tlvFile.read(3)
That's what I was trying to avoid, those two reads.
Otherwise, why not just read lines and use the free iterator:
for line in tlvFile.readlines(): print line
Because the file doesn't have lines, it's just a stream of bytes.