473,387 Members | 1,942 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Insert Content of a File into a Variable


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
3 2131
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
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Fang | last post by:
I want to open a file and write some string between the the 3rd line and the 4th line. my code (as follows) can insert after the 3rd line but somehow it owerwrites the 4th line. So i lost its...
2
by: help | last post by:
I want to open a file and write some string between the the 3rd line and the 4th line. my code (as follows) can insert after the 3rd line but somehow it owerwrites the 4th line. So i lost its...
16
by: Philip Boonzaaier | last post by:
I want to be able to generate SQL statements that will go through a list of data, effectively row by row, enquire on the database if this exists in the selected table- If it exists, then the colums...
7
by: David Bear | last post by:
I have a dictionary that contains a row of data intended for a data base. The dictionary keys are the field names. The values are the values to be inserted. I am looking for a good pythonic...
7
by: Wojtek | last post by:
Hello, how can I transparently insert one XML file into another? I am looking for a method simmilar to the #include <file> known from the C language. To be more precise, an example: I have...
9
by: anachronic_individual | last post by:
Hi all, Is there a standard library function to insert an array of characters at a particular point in a text stream without overwriting the existing content, such that the following data in...
1
by: cricrin | last post by:
Hello guys! This is Cristian From Argentina, and I wanted to ask you some help, I've looking on this and it makes me mad, I found that the error is in the $content , when y try to insert the...
0
ak1dnar
by: ak1dnar | last post by:
There is a Error getting while i am entering records using this jsp file. <%@ page contentType="text/html; charset=iso-8859-1" language="java" import="java.sql.*" errorPage="" %> <%@ include...
10
by: electric_r | last post by:
Hallo, I have 2 questions: with php and mysql : 1) How can I insert a text file into a TEXT field of a table ? 2) How can I insert a microsoft word file into a BLOB field of a table ?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.