Insert Content of a File into a Variable

W

Wijaya Edward

Hi,

How can we slurp all 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.
--------------------------------------------------------
 
C

Cameron Walsh

Wijaya said:
Hi,

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

I tried this:

<open file 'somefile.txt', mode 'r' at 0xb7f532e0>


But it doesn't print the content of the file.
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 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:
['__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']
Ye gads! I wish I'd chosen a shorter file! Or used a variable to put

Ye gads! I wish I'd chosen a shorter file! <SNIP>


You might also try:
<SNIP lots of lines>

Hope it helps,

Cameron.
 
P

Paul McGuire

Wijaya Edward said:
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
 

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

Staff online

Members online

Forum statistics

Threads
473,991
Messages
2,570,217
Members
46,805
Latest member
ClydeHeld1

Latest Threads

Top