473,666 Members | 2,678 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1929

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
1389
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
2698
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 can place at the top of the page to override the size limitation? Thanks.
4
5889
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 = window.open("www.yahoo.com","");
2
2379
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 we modified the links that invoke the javascripts from: <a href=javascript: top.doSomething> text </a> To: <span onclick=javascript:top.doSomething> text </span> The javascripts worked properly. Any ideas on why this now works or other...
1
2331
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 rebuild the indexes on a nightly basis. We cannot determine if Access is the culprit and I have not been able to look at the Access reports and determine how he is linking to the tables. I realize this is not much to go on, but does anyone see any...
9
2902
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
5549
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" + "\\root\\CIMV2:Win32_Process");
2
1591
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? Thanks. Here are some of the URL characters in the parameters
11
1849
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, I'm still not able to delete a record. The code I am using is as follows:-
5
2319
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 logged in to a particular website at the current moment. Would it cause me trouble if I made this array static? If it would cause trouble, what's the easiest way to persist the data? I could use a database, but that seems heavy handed for a 20...
0
8779
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8549
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8636
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7376
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6187
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5660
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4186
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4356
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2004
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.