By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,965 Members | 1,962 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,965 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Jul 18 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a

Sara Khalatbari wrote:
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.

Jul 18 '05 #2

P: n/a

Sara Khalatbari wrote:
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


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


Jul 18 '05 #3

P: n/a
Fuzzyman wrote:
Sara Khalatbari wrote:
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...


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
Jul 18 '05 #4

P: n/a
Daniel Dittmar wrote:
Fuzzyman wrote:
Sara Khalatbari wrote:
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...

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

Jul 18 '05 #5

P: n/a
Sara Khalatbari <sa************@yahoo.com> wrote:
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.
--
- Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Jul 18 '05 #6

P: n/a
Tim Roberts wrote:
Sara Khalatbari <sa************@yahoo.com> wrote:

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.


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

Jul 18 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.