[Tutor] re-reading file-like objects

Kent Johnson kent37 at tds.net
Tue Oct 10 02:57:29 CEST 2006


Tiago Saboga wrote:
> I'm afraid I don't fully understand file objects. I thought I could use it 
> just as a file: if I have one, and I want several copies, I just save it with 
> several names. So, if Popen.stdout already is a file object, I thought the 
> easier way would be to save it with another name. I see that it is more 
> complicated.

A file-like object doesn't represent a file on disk so much as a data 
stream. It is a place you can read data from or write data to. When you 
call subprocess.Popen(), you get back a data stream representing the 
output of the process you started. You can read through this stream 
once, when you get to the end it's done. Just like when you run the 
process from the console, it just outputs once. If you want to use the 
output more than once you can copy it or save it or run the process again.

Some file-like objects do let you seek to a specific location, but not all.

Kent



More information about the Tutor mailing list