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

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

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
6 1918

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

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

Similar topics

0
by: yukon | last post by:
Hi there the next is my platform IIS5 windows 2000 asp vbscript My working area has the next virtual directoy: testAc (corrective ambient) testAe (evolutionary ambient)
13
by: teenzbutler | last post by:
I have a 10mb ASP file, which will not load in IE 6.0. I get an HTTP 500 error. When I convert the 10mb file to HTML, it loads fine. Is there a limitation on file size? If so, is there a tag I...
4
by: OJ | last post by:
Hi, This works to maximize the window, but wants to load yahoo locally : C:\WINDOWS\Desktop\www.yahoo.com <html> <script type="text/javaScript"> <!-- function test() { qwe =...
2
by: sdvoranchik | last post by:
We have an application that contains links that run javascripts to create pages in a separate frame. When these links open an external site, it causes the javascripts to no longer function. When...
1
by: jdola | last post by:
I have a customer that is using Access to create reports from FoxPro tables. We started noticing problems with the indexes on the FoxPro tables. It is to the point where we have to delete and...
9
by: | last post by:
void show( char *s, ...) is a function seemd like prinf code -------------- #include <stdio.h> #include <stdarg.h> void show( char *s, ...) { va_list stage;
11
by: ricolee99 | last post by:
Hi everyone, I'm trying to invoke my .exe application from a remote server. Here is the code: ManagementClass processClass = new ManagementClass ("\\\\" +"RemoteServerName" +...
2
by: Joe | last post by:
Hi, I want to save some URLs into a XML formatted document. I find out that its having some problems due to some of the characters used in the URL. Is there a quick way to get around that? ...
11
by: Yvonne | last post by:
Hi, I'm running Access 2002 and have a problem deleting records on a continuous form. I thought it might be due to relationships with two other tables but having deleted these relationships,...
5
by: Lawrence Krubner | last post by:
Do any problems come up when using a static variable in a cron job? Assuming the cron job is called every 5 minutes for one year. Assume I've got an array that stores the names of which users are...
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: 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
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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,...
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.