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

Safely dealing with arbitrary file objects

I have found myself writing functions rather like these:

def openfile(filename):
if filename == '-':
# convention for shell scripts in Unix-land is to use
# '-' for stdin/stdout for reading/writing.
outfile = sys.stdout
if filename == '2-':
outfile = sys.stderr
else:
outfile = file(filename, 'w')
return outfile

def closefile(fileobj):
# don't close standard file objects, or their replacements
if not fileobj in (sys.stdout, sys.stderr, sys.stdin,
sys.__stdout__, sys.__stderr__, sys.__stdin__):
fileobj.close()
def processfile(filename):
outfile = openfile(filename)
try:
# lots of processing here, which may raise exceptions
var = "stuff happens"
outfile.write(var)
finally:
closefile(outfile)

A question:

I know I'm being paranoid about not closing files I shouldn't close, but
am I being excessively paranoid, or not paranoid enough?

Suggestions for improvements welcome; while I'm happy to read suggestions
using the new with statement, I can't yet rely on having Python 2.5 or
better so I have to stick to try...finally.
--
Steven.
Oct 14 '07 #1
2 1195
Steven D'Aprano wrote:
I have found myself writing functions rather like these:

def openfile(filename):
if filename == '-':
# convention for shell scripts in Unix-land is to use
# '-' for stdin/stdout for reading/writing.
outfile = sys.stdout
if filename == '2-':
outfile = sys.stderr
else:
outfile = file(filename, 'w')
return outfile

def closefile(fileobj):
# don't close standard file objects, or their replacements
if not fileobj in (sys.stdout, sys.stderr, sys.stdin,
sys.__stdout__, sys.__stderr__, sys.__stdin__):
fileobj.close()
def processfile(filename):
outfile = openfile(filename)
try:
# lots of processing here, which may raise exceptions
var = "stuff happens"
outfile.write(var)
finally:
closefile(outfile)

A question:

I know I'm being paranoid about not closing files I shouldn't close, but
am I being excessively paranoid, or not paranoid enough?

Suggestions for improvements welcome; while I'm happy to read suggestions
using the new with statement, I can't yet rely on having Python 2.5 or
better so I have to stick to try...finally.
How about a wrapper that passes along all but 'close', so you don't
have to worry about closing something you means to leave open.
Something like:

class DontCloseOutput(file):
'''subclass file only to allow isinstance checks to work out'''
def __init__(self, referent):
self._actual = referent
self.closed = False

def close(self):
self.closed = True

def write(self, data):
if self.closed:
raise IOError('Tried to write closed pseudo-file')
return self._actual.write(data)
def writeopen(filename):
if filename == '-':
# convention for shell scripts in Unix-land is to use
# '-' for stdin/stdout for reading/writing.
return DontCloseOutput(sys.stdout)
if filename == '2-':
return DontCloseOutput(sys.stderr)
else:
return open(filename, 'w') # BTW, open is the preferred way
# file was for a short time.

You of course can do similar things (probably forwarding more message)
with readopen.

-Scott David Daniels
Sc***********@Acm.Org
Oct 14 '07 #2
On Oct 14, 5:01 am, Steven D'Aprano <st...@REMOVE-THIS-
cybersource.com.auwrote:
I have found myself writing functions rather like these:

def openfile(filename):
if filename == '-':
# convention for shell scripts in Unix-land is to use
# '-' for stdin/stdout for reading/writing.
outfile = sys.stdout
if filename == '2-':
outfile = sys.stderr
else:
outfile = file(filename, 'w')
return outfile

def closefile(fileobj):
# don't close standard file objects, or their replacements
if not fileobj in (sys.stdout, sys.stderr, sys.stdin,
sys.__stdout__, sys.__stderr__, sys.__stdin__):
fileobj.close()

def processfile(filename):
outfile = openfile(filename)
try:
# lots of processing here, which may raise exceptions
var = "stuff happens"
outfile.write(var)
finally:
closefile(outfile)

A question:

I know I'm being paranoid about not closing files I shouldn't close, but
am I being excessively paranoid, or not paranoid enough?

Suggestions for improvements welcome; while I'm happy to read suggestions
using the new with statement, I can't yet rely on having Python 2.5 or
better so I have to stick to try...finally.
With with:

import sys

class leave_open(object):
def __init__(self, obj):
self.obj = obj
def __enter__(self):
return self.obj
def __exit__(self, *exc_info):
pass

def open_file(name, special={'-':sys.stdout, '2-':sys.stderr}):
if name in special:
return leave_open(special[name])
else:
return open(name, 'w')

def process_file(filename):
with open_file(filename) as outfile:
outfile.write("stuff happens")
Without with:

import sys

def open_file(name, special={'-':sys.stdout, '2-':sys.stderr}):
if name in special:
return False, special[name]
return True, open(name, 'w')

def process_file(filename):
should_close, outfile = open_file(filename)
try:
outfile.write('stuff happens')
finally:
if should_close:
outfile.close()

--
Paul Hankin

Oct 14 '07 #3

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

Similar topics

3
by: Bret Pehrson | last post by:
I'm working on a (non-trivial) project which includes various & numerous objects, some in my direct source control, some not. I'm finding the need to somehow associate one object with another,...
1
by: Patrick | last post by:
Hi, This post is the 'sequel' ;) of the "Data Oriented vs Object Oriented Design" post, but it can be read and treated apart from that one. I will just quote the beginning of my previous message...
4
by: Russell Warren | last post by:
I've got a case where I want to convert binary blocks of data (various ctypes objects) to base64 strings. The conversion calls in the base64 module expect strings as input, so right now I'm...
27
by: pkirk25 | last post by:
Assume an array of structs that is having rows added at random. By the time it reaches your function, you have no idea if it has a few hundred over over 10000 rows. When your function recieves...
17
by: Christopher Benson-Manica | last post by:
Some recent posts got me thinking about how one might have dealt with simplistic malloc() implementations which might return NULL for a 64K request but might accept two 32K requests or four 16K...
28
by: walterbyrd | last post by:
Python seems to have a log of ways to do collections of arbitrary objects: lists, tuples, dictionaries. But what if I want a collection of non-arbitrary objects? A list of records, or something...
3
by: sophie_newbie | last post by:
Hi, I want to store python text strings that characters like "é" "Č" in a mysql varchar text field. Now my problem is that mysql does not seem to accept these characters. I'm wondering if there...
4
by: arnaudk | last post by:
I am trying to come up with a class design to deal with asynchronous data to be stored and analyzed over multiple time frames and could really use some design input. This is a rather long question...
16
by: Fett | last post by:
I am creating a program that requires some data that must be kept up to date. What I plan is to put this data up on a web-site then have the program periodically pull the data off the web-site. ...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: 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...

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.