Reading a file into a Python string


I’ve learned a number of useful things from the Google learn Python video series. One of the tips I got to use today. That tip was Python’s ability to read a file into a string:

$ cat foo

this
is
a
test
file
of
words

$ python

>>> f = open("foo","r")
>>> string = f.read()
>>> string
'this
is 
a 
test
file
of 
words
'

This has a few interesting uses, and I plan to put this to use this weekend when I finish up a Python project I’m working on. I really, really dig Python. It’s quite swell. :)

This article was posted by Matty on 2012-01-30 21:44:00 -0400 -0400