views:

237

answers:

3

I know there is a StringIO stream in Python, but is there such a thing as a file stream in Python? Also is there a better way for me to look up these things? Documentation, etc...

I am trying to pass a "stream" to a "writer" object I made. I was hoping that I could pass a file handle/stream to this writer object.

+5  A: 

There is a builtin file() which works much the same way. Here are the docs: http://docs.python.org/library/functions.html#file and http://python.org/doc/2.5.2/lib/bltin-file-objects.html.

If you want to print all the lines of the file do:

for line in file('yourfile.txt'):
  print line

Of course there is more, like .seek(), .close(), .read(), .readlines(), ... basically the same protocol as for StringIO.

Edit: You should use open() instead of file(), which has the same API - file() goes in Python 3.

bayer
The file object is documented here: http://docs.python.org/library/stdtypes.html#bltin-file-objects
tsg
+6  A: 

I am guessing you are looking for open(). http://docs.python.org/library/functions.html#open

outfile = open("/path/to/file", "w")
[...]
outfile.write([...])

Documentation on all the things you can do with streams (these are called "file objects" or "file-like objects" in Python): http://docs.python.org/library/stdtypes.html#file-objects

Phil
+1  A: 

In Python, all the I/O operations are wrapped in a hight level API : the file likes objects.

It means that any file likes object will behave the same, and can be used in a function expecting them. This is called duck typing, and for file like objects you can expect the following behavior :

  • open / close / IO Exceptions
  • iteration
  • buffering
  • reading / writing / seeking

StringIO, File, and all the file like objects can really be replaced with each others, and you don't have to care about managing the I/O yourself.

As a little demo, let's see what you can do with stdout, the standard output, which is a file like object :

import sys
# replace the standar ouput by a real opened file
sys.stdout = open("out.txt", "w")
# printing won't print anything, it will write in the file
print "test"

All the file like objects behave the same, and you should use them the same way :

# try to open it
# do not bother with checking wheter stream is available or not

try :
    stream = open("file.txt", "w")
except IOError :
    # if it doesn't work, too bad !
    # this error is the same for stringIO, file, etc
    # use it and your code get hightly flexible !
    pass
else :
    stream.write("yeah !")
    stream.close()

# in python 3, you'd do the same using context :

with open("file2.txt", "w") as stream :
    stream.write("yeah !")

# the rest is taken care automatically

Note that a the file like objects methods share a common behavior, but the way to create a file like object is not standard :

import urllib
# urllib doesn't use "open" and doesn't raises only IOError exceptions
stream = urllib.urlopen("www.google.com")

# but this is a file like object and you can rely on that :
for line in steam :
    print line

Un last world, it's not because it works the same way that the underlying behavior is the same. It's important to understand what you are working with. In the last example, using the "for" loop on an Internet resource is very dangerous. Indeed, you know is you won't end up with a infinite stream of data.

In that case, using :

print steam.read(10000) # another file like object method

is safer. Hight abstractions are powerful, but doesn't save you the need to know how the stuff works.

e-satis