By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,838 Members | 1,606 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,838 IT Pros & Developers. It's quick & easy.

Insert Content of a File into a Variable

P: n/a

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.

Regards,
-- Edward WIJAYA
SINGAPORE

------------ Institute For Infocomm Research - Disclaimer -------------
This email is confidential and may be privileged. If you are not the intended recipient, please delete it and notify us immediately. Please do not copy or use it for any purpose, or disclose its contents to any other person. Thank you.
--------------------------------------------------------
Oct 27 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On 2006-10-27, Wijaya Edward <ew*****@i2r.a-star.edu.sgwrote:
How can we slurp all content of a single file
into one variable?
http://docs.python.org/tut/node9.htm...00000000000000

I suggest you read the rest of the tutorial as well.

--
Grant Edwards grante Yow! I want EARS! I
at want two ROUND BLACK
visi.com EARS to make me feel warm
'n secure!!
Oct 27 '06 #2

P: n/a
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.
>>help(open)
Help on built-in function open in module __builtin__:

open(...)
open(name[, mode[, buffering]]) -file object

Open a file using the file() type, returns a file object.
>>help(file)
Help on class file in module __builtin__:

class file(object)
| file(name[, mode[, buffering]]) -file object

<SNIP>

|read(...)
| read([size]) -read at most size bytes, returned as a string.
|
| If the size argument is negative or omitted, read until EOF is
| reached.
| Notice that when in non-blocking mode, less data than what was
| requested
| may be returned, even if no size parameter was given.

<SNIP>

|readlines(...)
| readlines([size]) -list of strings, each a line from the file.
|
| Call readline() repeatedly and return a list of the lines so read.
| The optional size argument, if given, is an approximate bound on the
| total number of bytes in the lines returned.

<SNIP>

Those sound useful...

Or alternatively:
>>my_file = open('somefile.txt')
print myfile_content,
<open file 'somefile.txt', mode 'r' at 0xb7f532e0>
>>#Hmmm, that's not what I wanted, what can I do with this?
dir(my_file)
['__class__', '__delattr__', '__doc__', '__enter__', '__exit__',
'__getattribute__', '__hash__', '__init__', '__iter__', '__new__',
'__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__str__',
'close', 'closed', 'encoding', 'fileno', 'flush', 'isatty', 'mode',
'name', 'newlines', 'next', 'read', 'readinto', 'readline', 'readlines',
'seek', 'softspace', 'tell', 'truncate', 'write', 'writelines',
'xreadlines']
>>#That read attribute looks interesting...
my_file.read
<built-in method read of file object at 0x011750B0>
>>my_file.read()
Ye gads! I wish I'd chosen a shorter file! Or used a variable to put
it in! <SNIP>
>>text = my_file.read()
print text
Ye gads! I wish I'd chosen a shorter file! <SNIP>
You might also try:
>>lines = my_file.readlines()
for line in lines:
print line
<SNIP lots of lines>

Hope it helps,

Cameron.
Oct 27 '06 #3

P: n/a
"Wijaya Edward" <ew*****@i2r.a-star.edu.sgwrote in message
news:ma***************************************@pyt hon.org...
>
Hi,

How can we slurp all content of a single file
into one variable?
Please don't double-post your questions. Now you have two threads running
with people answering the same question.

-- Paul
Oct 27 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.