473,406 Members | 2,404 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,406 software developers and data experts.

When is a directly used file object closed?

27
In the line:

Expand|Select|Wrap|Line Numbers
  1. open("file","a").write("\n")
an object is created, but where does it go afterwards, and is the file "closed" like in:

Expand|Select|Wrap|Line Numbers
  1. f = open('file', 'a')
  2. f.close()
  3.  
Is there a problem in not closing files?
Jul 8 '07 #1
3 1220
bvdet
2,851 Expert Mod 2GB
In the line:

Expand|Select|Wrap|Line Numbers
  1. open("file","a").write("\n")
an object is created, but where does it go afterwards, and is the file "closed" like in:

Expand|Select|Wrap|Line Numbers
  1. f = open('file', 'a')
  2. f.close()
  3.  
Is there a problem in not closing files?
When reading a file, it is not necessary to create a file object as in:
Expand|Select|Wrap|Line Numbers
  1. lineList = open('filename').readlines()
When writing or appending to a file, I recommend that you create a file object and explicitly close the object when you are through. That forces the system to flush the output buffers and finish writing the data.
Jul 8 '07 #2
bartonc
6,596 Expert 4TB
In the line:

Expand|Select|Wrap|Line Numbers
  1. open("file","a").write("\n")
an object is created, but where does it go afterwards, and is the file "closed" like in:

Expand|Select|Wrap|Line Numbers
  1. f = open('file', 'a')
  2. f.close()
  3.  
Is there a problem in not closing files?
Although it is not necessary to explicitly close a file, it is good practice to do so.
The file will (if all goes well) be closed when the the object is garbage collected.
If you open a file without keeping a reference to it, there is still an object created which is immediately marked for garbage collection. I cringe when I see people doing this.
If you do keep a reference as in
Expand|Select|Wrap|Line Numbers
  1. f = open('filename')
then the file will remain open as long as that reference exists.

Best practice: call close() as soon as you are done with the file.
Jul 8 '07 #3
ghostdog74
511 Expert 256MB
In the line:

Expand|Select|Wrap|Line Numbers
  1. open("file","a").write("\n")
an object is created, but where does it go afterwards, and is the file "closed" like in:
yes, its implicitly closed.
Jul 9 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Jonathan | last post by:
I am trying to upload a new version of a file that already exists on my web site. It asks me to overwrite and I choose yes. But when I go to the web page, even a half hour after the upload, the...
10
by: Not Available | last post by:
On the host server: namespace JCart.Common public class JCartConfiguration : IConfigurationSectionHandler private static String dbConnectionString; public static String ConnectionString { get...
6
by: Ana | last post by:
Hi! I have problems with the following scenario: My application is developed using C# under .NET. It must run on all Windows versions starting from Windows 98. The user must open different...
9
by: tshad | last post by:
This is from my previous post, but a different issue. I have the following Javascript routine that opens a popup page, but doesn't seem to work if called from an asp.net button. It seems to work...
3
by: Amar | last post by:
I have a abc.PRG file in visual foxpro 8.0. I can run this file using visual foxpro environment and it creates a table X.dbf in the same folder where this program file is and populates some data...
46
by: clintonG | last post by:
Documentation tells me how but not when, why or where... <%= Clinton Gallagher http://msdn2.microsoft.com/en-us/library/saxz13w4(VS.80).aspx
2
by: =?Utf-8?B?U3VuaWwgUGFuZGl0YQ==?= | last post by:
Hi All, I have created a VB.Net application that used classic ADO to access the oracle database. I have mentained a persistant connection in it. When i start the application and it works fine....
14
by: =?Utf-8?B?UGF1bA==?= | last post by:
Hi I tried a google search but could not find anything. I am trying to cause one webpage to reload when a second web page is closed. The second webpage loads data into a session variable and when...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
0
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...
0
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...

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.