will it cause any problems to open a read-only file & not close it?

S

Sara Khalatbari

Dear friends
In a code, I'm opening a file to read. Like :
lines = open(filename).readlines()
& I'm never closing it.
I'm not writing in that file, I just read it.

Will it cause any problems if you open a file to read
& never close it?




__________________________________
Do you Yahoo!?
Make Yahoo! your home page
http://www.yahoo.com/r/hs
 
J

John Machin

Sara said:
Dear friends
In a code, I'm opening a file to read. Like :
lines = open(filename).readlines()
& I'm never closing it.
I'm not writing in that file, I just read it.

Will it cause any problems if you open a file to read
& never close it?

AFAIK, only if you try to have too many files open at one time -- many
appears to be approx 512 on recent Windows (2000 and XP at least).

However it's a very good habit whatever the language to close / delete
/ deallocate / ... any resource as soon as you no longer need it.
 
F

Fuzzyman

Sara said:
Dear friends
In a code, I'm opening a file to read. Like :
lines = open(filename).readlines()
& I'm never closing it.
I'm not writing in that file, I just read it.

Will it cause any problems if you open a file to read
& never close it?

Under CPython the filehandle will be automatically garbage collected.
Under JPython (Jython) it won't be... It's a very useful shortcut
though *sigh*

Regards,

Fuzzy
http://www.voidspace.org.uk/python/index.shtml
 
D

Daniel Dittmar

Fuzzyman said:
Under CPython the filehandle will be automatically garbage collected.
Under JPython (Jython) it won't be...

Isn't it rather that CPython will close the file as soon as the last
reference is dropped, which is probably right after the read returns?

Whereas Jython/Java will close the file, when the garbage collector
actually reclaims the object, which will be later. Or much later.
> It's a very useful shortcut
> though *sigh*

It isn't that difficult to write a small function to that effect.

It would perhaps be wise to acknowledge the existence of Jython and
IronPython by allowing an additional flag for open to the effect "close
when the end of the file is reached". This would prevent similar
problems with iter (open (...)), which can't be wrapped quite as easily.

Daniel
 
J

Jeff Shannon

Daniel said:
Isn't it rather that CPython will close the file as soon as the last
reference is dropped, which is probably right after the read returns?

Whereas Jython/Java will close the file, when the garbage collector
actually reclaims the object, which will be later. Or much later.

That's my understanding. In either case, it will only cause
"problems" if you're doing something else with that file immediately
afterwards. In the case of files that you're writing, this includes
reading the file from another program -- a file that hasn't been
closed probably also hasn't been flushed to disk, and therefore (IIUC)
the file is not reliably readable until the Jython/Java program has
terminated (or GC can otherwise be guaranteed to have run).

However, in the case where you're simply reading the contents of a
file and don't need to do anything else with the disk file or file
object after that, then this idiom should be harmless even under Jython.

Jeff Shannon
 
T

Tim Roberts

Sara Khalatbari said:
Dear friends
In a code, I'm opening a file to read. Like :
lines = open(filename).readlines()
& I'm never closing it.
I'm not writing in that file, I just read it.

Will it cause any problems if you open a file to read
& never close it?

A file is closed when the last reference to it is deleted. Since you never
save a reference to this file, the last reference is deleted as soon as the
readlines() call finishes.

So, the file will be closed when you move to the next statement.
 
J

Jeff Shannon

Tim said:
A file is closed when the last reference to it is deleted. Since you never
save a reference to this file, the last reference is deleted as soon as the
readlines() call finishes.

So, the file will be closed when you move to the next statement.

This is true in current versions of CPython, but is not necessarily
true in all implementations of Python. In particular, Jython uses
Java's garbage collector; an object becomes available for collection
when the last reference is deleted, but that collection may not
(probably won't) happen right away. Since the automatic file closing
happens as part of the object deletion, files opened in this way won't
be closed until the garbage collector runs (and collects this file
object).

Most of the time, this won't be a problem, but it's good to be aware
that things are not necessarily as cut-and-dried as they might seem.

Jeff Shannon
 

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,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top