Insert Content of a File into a Variable

Steve Holden steve at holdenweb.com
Thu Oct 26 22:00:03 EDT 2006


Wijaya Edward wrote:
> Hi,
>  
> How can we slurp all content of a single file 
> into one variable?
>  
> I tried this:
>  
> 
>>>>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.
>  
Because you haven't read it!

data = open('somefile.txt').read()
print data

regards
  Steve
-- 
Steve Holden       +44 150 684 7255  +1 800 494 3119
Holden Web LLC/Ltd          http://www.holdenweb.com
Skype: holdenweb       http://holdenweb.blogspot.com
Recent Ramblings     http://del.icio.us/steve.holden




More information about the Python-list mailing list