Slurping All Content of a File into a Variable

Paul McGuire ptmcg at austin.rr._bogus_.com
Fri Oct 27 09:38:36 EDT 2006


<marc.wyburn at googlemail.com> wrote in message 
news:1161948531.098283.162950 at b28g2000cwb.googlegroups.com...
> myfile_content is an object and you have only opened the file.  Python
> doesn't yet know whether you want to read it, copy it etc.
>
> to read it try
>
> text = myfile_content.readlines()
> print text
>
> this should be in most tutorials
>

readlines() will give you the file content as a list of newline-terminated 
strings.  If you just want the whole file in one big string do:

text = myfile_content.read()

And, yes, you should wade through some of the tutorials, this is basic 
material.

-- Paul 





More information about the Python-list mailing list