file io - Python read() function returns empty string -


if type in python:

open("file","r").read() 

sometimes returns exact content of file string, other times returns empty string (even if file not empty). can explain depend on?

when reach end of file (eof) , .read method returns '', there no more data read.

>>> f = open('my_file.txt') >>> f.read() # read entire file 'my file has data.' >>> f.read() # you've reached end of file  '' >>> f.tell() # give current position @ file 17 >>> f.seek(0) # go starting position >>> f.read() # , read file again 'my file has data.' 

doc links: read() tell() seek()

note: if happens @ first time read file, check file not empty. if it's not try putting file.seek(0) before read.


Comments

Popular posts from this blog

Perl - how to grep a block of text from a file -

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -