Insert Content of a File into a Variable

Ben Finney bignose+hates-spam at benfinney.id.au
Fri Oct 27 04:34:12 CEST 2006


Wijaya Edward <ewijaya at i2r.a-star.edu.sg> writes:

> >>> myfile_content = open('somefile.txt')
> >>> print myfile_content,
> <open file 'somefile.txt', mode 'r' at 0xb7f532e0>
> >>> 
>
> But it doesn't print the content of the file.

That's right, because "the content of the file" is not what open() is
documented to return; it is documented as returning a 'file' object.

The 'read' method of the resulting file object will return the
contents of the file. You may also want to read the documentation of
thile objects to see what else you can do with them.

-- 
 \       "A free society is one where it is safe to be unpopular."  -- |
  `\                                             Adlai Ewing Stevenson |
_o__)                                                                  |
Ben Finney




More information about the Python-list mailing list