reading files

J

Johhny

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

bonono

Johhny said:
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 ?
 
S

Steve Holden

Johhny said:
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
 
J

Jorge Godoy

Johhny said:
===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.
 
B

bonono

Steve said:
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.
 
J

Johhny

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 ?
 
B

bonono

Johhny said:
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.
 
S

Steven D'Aprano

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

Fuzzyman

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
 
M

Mike Meyer

Steven D'Aprano said:
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
 
M

Marc 'BlackJack' Rintsch

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
 

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,780
Messages
2,569,611
Members
45,281
Latest member
Pedroaciny

Latest Threads

Top