Slurping All Content of a File into a Variable

W

Wijaya Edward

Hi,

How can we slurp content of a single file
into one variable?

I tried this:

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.
--------------------------------------------------------
 
M

marc.wyburn

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
 
P

Paul McGuire

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,992
Messages
2,570,220
Members
46,805
Latest member
ClydeHeld1

Latest Threads

Top