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

Open and closing files

Is it defined behaviour that all files get implicitly closed when not
assigning them?

Like:

def writeFile(fName, foo):
open(fName, 'w').write(process(foo))

compared to:
def writeFile(fName, foo):
fileobj = open(fName, 'w')
fileobj.write(process(foo))
fileobj.close()

Which one is the 'official' recommended way?

Thomas
Dec 1 '06 #1
2 1420

Thomas Ploch wrote:
Is it defined behaviour that all files get implicitly closed when not
assigning them?

Like:

def writeFile(fName, foo):
open(fName, 'w').write(process(foo))

compared to:
def writeFile(fName, foo):
fileobj = open(fName, 'w')
fileobj.write(process(foo))
fileobj.close()

Which one is the 'official' recommended way?
No such thing as an 'official' way.

Nothing happens until the file object is garbage-collected. GC is
generally not under your control.

Common sense suggests that
(a) when you are reading multiple files, you close each one explicitly
(b) when you are writing a file, you close it explicitly as soon as you
are done with it. That way you can trap any error condition and do
something moderately sensible -- better than getting an error condition
during GC when your Python process is shutting down.

HTH,
John

Dec 1 '06 #2

John Machin wrote:
Thomas Ploch wrote:
Is it defined behaviour that all files get implicitly closed when not
assigning them?

Like:

def writeFile(fName, foo):
open(fName, 'w').write(process(foo))

compared to:
def writeFile(fName, foo):
fileobj = open(fName, 'w')
fileobj.write(process(foo))
fileobj.close()

Which one is the 'official' recommended way?

No such thing as an 'official' way.

Nothing happens until the file object is garbage-collected. GC is
generally not under your control.

Common sense suggests that
(a) when you are reading multiple files, you close each one explicitly
(b) when you are writing a file, you close it explicitly as soon as you
are done with it. That way you can trap any error condition and do
something moderately sensible -- better than getting an error condition
during GC when your Python process is shutting down.

HTH,
John
Not to mention you can get bitten on the ass by a few characters
sitting in a buffer someplace when you expect your file to have been
fully written.

Close the thing.

Cheers,
-T

Dec 1 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Mike James | last post by:
First, the question - I remember reading in a bug report that there's a limit on the number of files you can open in Windows using Sun Java (around 2040 files). Can anyone verify this - or point me...
6
by: Sara Khalatbari | last post by:
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...
6
by: Mark Broadbent | last post by:
Everything I have read suggests that if I open files or database connections or the like, that I should explicitly close those said resources before making the object subject to GC. This is...
4
by: YYZ | last post by:
In my app, I am displaying PDF files to the user. I'm using the COM component "Adobe Acrobat 7.0 Browser Document" component. This works great. Just open up my form, then have it tall the PDF...
7
by: Tyrone Showers | last post by:
I have a problem of getting the error "too many files open" and would like to trace my application. However, I have found nothing about how to display the current number of open files. Does...
13
by: mom.klaib | last post by:
Hi I am a student doing a project using C++ builder. I hava a problem in opening a number of files more than 47, when run it only it create 46 file, Please help me to be able to create any number...
5
by: alexteo21 | last post by:
I have created a script using python that will batch process data files every hour The script is running on Solaris. Python version 2.3.3 t=open(filename,'rb') data=t.read() #processing...
6
by: AMD | last post by:
Hello, I need to split a very big file (10 gigabytes) into several thousand smaller files according to a hash algorithm, I do this one line at a time. The problem I have is that opening a file...
6
by: sebastian.noack | last post by:
Hi, is there a way to or at least a reason why I can not use tarfile to create a gzip or bunzip2 compressed archive in the memory? You might might wanna answer "use StringIO" but this isn't...
5
by: Ruben | last post by:
Hello, I am using the "Application.FollowHyperlink strFilePath, , True" line of code from within access forms to launch any file type I want (e.g., xls, doc, pdf, etc.). The files open up okay,...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.