473,385 Members | 1,798 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,385 software developers and data experts.

reading files

Hello All,

I am working my way through learning python as a language. I am having
some issues with something that looks right and does not work. I am
trying to get myself more familure with reading files. Based on the
tutorials at www.python.org This "should" work. but im not sure what
the issue is.

===SNIP===
import string

vsftpd=open('vsftpd.conf', 'r')
print vsftpd
vsftpd.read()
vsftpd.readlines()

vsftpd.close()
===SNIP===

When I check the permissions to ensure they are correct I get the
following.

stat vsftpd.conf
File: `vsftpd.conf'
Size: 4137 Blocks: 16 IO Block: 131072 regular
file
Device: 802h/2050d Inode: 51010 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 1000/ testing) Gid: ( 1000/
testing)
Access: 2005-12-19 10:21:04.000000000 +0000
Modify: 2005-12-16 12:34:00.000000000 +0000
Change: 2005-12-16 12:34:00.000000000 +0000

When I run the script I get the following:

python reading_file.py
<open file 'vsftpd.conf', mode 'r' at 0xb7d742a8>

Does anyone have any advice on this issue at all.

Regards,

Johhny.

Dec 19 '05 #1
10 2095

Johhny wrote:
Hello All,

I am working my way through learning python as a language. I am having
some issues with something that looks right and does not work. I am
trying to get myself more familure with reading files. Based on the
tutorials at www.python.org This "should" work. but im not sure what
the issue is.

===SNIP===
import string

vsftpd=open('vsftpd.conf', 'r')
print vsftpd
vsftpd.read()
vsftpd.readlines()

vsftpd.close()
===SNIP===

When I check the permissions to ensure they are correct I get the
following.

stat vsftpd.conf
File: `vsftpd.conf'
Size: 4137 Blocks: 16 IO Block: 131072 regular
file
Device: 802h/2050d Inode: 51010 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 1000/ testing) Gid: ( 1000/
testing)
Access: 2005-12-19 10:21:04.000000000 +0000
Modify: 2005-12-16 12:34:00.000000000 +0000
Change: 2005-12-16 12:34:00.000000000 +0000

When I run the script I get the following:

python reading_file.py
<open file 'vsftpd.conf', mode 'r' at 0xb7d742a8>

Does anyone have any advice on this issue at all.

Regards,

out of curiosity, what other programming background do you have prior
to use python ?

Dec 19 '05 #2
Johhny wrote:
Hello All,

I am working my way through learning python as a language. I am having
some issues with something that looks right and does not work. I am
trying to get myself more familure with reading files. Based on the
tutorials at www.python.org This "should" work. but im not sure what
the issue is.

===SNIP===
import string

vsftpd=open('vsftpd.conf', 'r')
print vsftpd
vsftpd.read()
vsftpd.readlines()

vsftpd.close()
===SNIP===

When I check the permissions to ensure they are correct I get the
following.

stat vsftpd.conf
File: `vsftpd.conf'
Size: 4137 Blocks: 16 IO Block: 131072 regular
file
Device: 802h/2050d Inode: 51010 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 1000/ testing) Gid: ( 1000/
testing)
Access: 2005-12-19 10:21:04.000000000 +0000
Modify: 2005-12-16 12:34:00.000000000 +0000
Change: 2005-12-16 12:34:00.000000000 +0000

When I run the script I get the following:

python reading_file.py
<open file 'vsftpd.conf', mode 'r' at 0xb7d742a8>

Does anyone have any advice on this issue at all.

Yes: Python is doing exactly what you've told it to.

vsftpd is a file object, and what you are seeing is the representation
(repr()) of that object.

Try

print vsftpd.read()

instead and you'll see the content of the file.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC www.holdenweb.com
PyCon TX 2006 www.python.org/pycon/

Dec 19 '05 #3
"Johhny" <ex*****@hotmail.com> writes:
===SNIP===
import string
Why's that here? You don't need that import...
vsftpd=open('vsftpd.conf', 'r')
Open the file and associate its resulting *object* to the 'vsftpd' variable.
print vsftpd
Print the object's __str__.
vsftpd.read()
Read the full file into the string at the LHS (left side of the '='). In your
case, it reads the full file and discard its contents.
vsftpd.readlines()
Read all other lines (none) into an array on the LHS.
vsftpd.close()
Close the file.
When I run the script I get the following:

python reading_file.py
<open file 'vsftpd.conf', mode 'r' at 0xb7d742a8>

Does anyone have any advice on this issue at all.


What issue? You did nothing with what you read, just with the object itself.
--
Jorge Godoy <go***@ieee.org>
Dec 19 '05 #4

Steve Holden wrote:
Johhny wrote:
Hello All,

I am working my way through learning python as a language. I am having
some issues with something that looks right and does not work. I am
trying to get myself more familure with reading files. Based on the
tutorials at www.python.org This "should" work. but im not sure what
the issue is.

===SNIP===
import string

vsftpd=open('vsftpd.conf', 'r')
print vsftpd
vsftpd.read()
vsftpd.readlines()

vsftpd.close()
===SNIP===

When I check the permissions to ensure they are correct I get the
following.

stat vsftpd.conf
File: `vsftpd.conf'
Size: 4137 Blocks: 16 IO Block: 131072 regular
file
Device: 802h/2050d Inode: 51010 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 1000/ testing) Gid: ( 1000/
testing)
Access: 2005-12-19 10:21:04.000000000 +0000
Modify: 2005-12-16 12:34:00.000000000 +0000
Change: 2005-12-16 12:34:00.000000000 +0000

When I run the script I get the following:

python reading_file.py
<open file 'vsftpd.conf', mode 'r' at 0xb7d742a8>

Does anyone have any advice on this issue at all.

Yes: Python is doing exactly what you've told it to.

vsftpd is a file object, and what you are seeing is the representation
(repr()) of that object.

Try

print vsftpd.read()

instead and you'll see the content of the file.

The obvious is not so obvious for this user.

Dec 19 '05 #5
Thanks for your assistance, Is it proper practice in python to flush
any memory when you exit? for example Ive read the file into memory,
when I close the file do I also have to flush any memory allocations ?

Dec 19 '05 #6

Johhny wrote:
Thanks for your assistance, Is it proper practice in python to flush
any memory when you exit? for example Ive read the file into memory,
when I close the file do I also have to flush any memory allocations ?

No. you don't need to and shouldn't unless you have very very specific
need to "del" things and even that is not flush memory.

Dec 19 '05 #7
On Mon, 19 Dec 2005 02:47:22 -0800, Johhny wrote:
Thanks for your assistance, Is it proper practice in python to flush
any memory when you exit? for example Ive read the file into memory,
when I close the file do I also have to flush any memory allocations ?


You've done this:

fileobject = file("my file", "r")
mytext = fileobject.read()
fileobject.close()

(It is good practice to close the file as soon as you can, especially on
platforms like Windows where open files can't be read by any other process.)

The text you read is still hanging around, waiting for you to use it. Once
you are done with it, you might want to reclaim that memory used,
especially if it is a 100MB text file:

mytext = "" # re-bind the name 'mytext' to the empty string

Now that enormous chunk of text will be collected by the Python garbage
collector, reclaiming the memory used.

But even that is not always needed: if the name 'mytext' is local to a
function, then when the function ends, the block of memory used will be
collected by the Python garbage collector, and you don't have to do a
thing.
--
Steven.

Dec 19 '05 #8
Steven D'Aprano wrote:
[snip..]
(It is good practice to close the file as soon as you can, especially on
platforms like Windows where open files can't be read by any other process.)


Unfortuantely not the case - on windoze (XP SP2 at least) you can open
a file in one process (for reading or writing) and still read (or write
to) it from another process.

FIle locking nightmare ahoy... You can obtain locks from the operating
system though.

All the best,

Fuzzyman
http://www.voidspace.org.uk/python/index.shtml

Dec 19 '05 #9
Steven D'Aprano <st***@REMOVETHIScyber.com.au> writes:
The text you read is still hanging around, waiting for you to use it. Once
you are done with it, you might want to reclaim that memory used,
especially if it is a 100MB text file:

mytext = "" # re-bind the name 'mytext' to the empty string
If you have no further use for the variable, then "del mytext" will
also free up the memory, and make it clear to the reader that you're
done with the memory.
But even that is not always needed: if the name 'mytext' is local to a
function, then when the function ends, the block of memory used will be
collected by the Python garbage collector, and you don't have to do a
thing.


The immediate collection is implementation-dependent. It works that
way in Cpython. Other implementations may leave the memory allocated
unil there's a GC run. Since I'm dealinng with trivia, whether or not
the OS gets the memory back or it stays part of the python process
varies from platform to platform and implementation to implementation.

<mike
--
Mike Meyer <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Dec 19 '05 #10
In <86************@bhuda.mired.org>, Mike Meyer wrote:
Steven D'Aprano <st***@REMOVETHIScyber.com.au> writes:
The text you read is still hanging around, waiting for you to use it. Once
you are done with it, you might want to reclaim that memory used,
especially if it is a 100MB text file:

mytext = "" # re-bind the name 'mytext' to the empty string


If you have no further use for the variable, then "del mytext" will
also free up the memory, and make it clear to the reader that you're
done with the memory.


It makes clear you're done with the *name* since that's what ``del``
deletes from the namespace, not the object. I know that you know this but
many people seem to think ``del`` get's rid of the object like a call to
`free()` in C.

Ciao,
Marc 'BlackJack' Rintsch
Dec 20 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Olivier Maurice | last post by:
Hi all, I suppose some of you know the program Redmon (type redmon in google, first result). This neat little tool allows to hook up any functionality to a printer by putting the file printed...
19
by: Lionel B | last post by:
Greetings, I need to read (unformatted text) from stdin up to EOF into a char buffer; of course I cannot allocate my buffer until I know how much text is available, and I do not know how much...
1
by: Magnus | last post by:
allrite folks, got some questions here... 1) LAY-OUT OF REPORTS How is it possible to fundamentaly change the lay-out/form of a report in access? I dont really know it that "difficult", but...
6
by: Rajorshi Biswas | last post by:
Hi folks, Suppose I have a large (1 GB) text file which I want to read in reverse. The number of characters I want to read at a time is insignificant. I'm confused as to how best to do it. Upon...
2
by: nnimod | last post by:
Hi. I'm having trouble reading some unicode files. Basically, I have to parse certain files. Some of those files are being input in Japanese, Chinese etc. The easiest way, I figured, to distinguish...
7
by: jccorreu | last post by:
I've got to read info from multiple files that will be given to me. I know the format and what the data is. The thing is each time we run the program we may be using a differnt number of files,...
6
by: arne.muller | last post by:
Hello, I've come across some problems reading strucutres from binary files. Basically I've some strutures typedef struct { int i; double x; int n; double *mz;
10
by: Tyler | last post by:
Hello All: After trying to find an open source alternative to Matlab (or IDL), I am currently getting acquainted with Python and, in particular SciPy, NumPy, and Matplotlib. While I await the...
5
blazedaces
by: blazedaces | last post by:
Ok, so you know my problem, java is running out of memory reading with SAX, the event-based xml parser intended more-so than DOM for extremely large files. I'll try to explain what I've been doing...
4
by: Miner Jeff | last post by:
Hello, I have a basic question about reading files. I have several data files where the filenames are identical except for a short (3 character) prefix. I inherited this code and the person who...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: 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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.