[Tutor] dynamically creating files
Daniel Ehrenberg
littledanehren at yahoo.com
Fri Nov 14 13:54:56 EST 2003
--- bob wrote:
> I am creating a program and have come to a problem.
> I wish to write a class that handles file objects,
> ie opening, writing to,
> and closing a file. I wish to dynamically create the
> file name, as each
> instance would have its own file too use for
> processing. I am not sure on
> what syntax is required to derive a filename from
> the 'self' instance.
> Any tips/ TIA
I think what you want to do is subclass the file type.
Here's a simple subtype of the file class that has
input from one file and output to another class. It
will act similarly to a file in rU+ mode.
class newfile(file):
def __init__(self, inputfile, outputfile):
self.inputfile = file(inputfile, 'rU')
self.outputfile = file(outputfile, 'w')
def read(self, *args):
return self.inputfile.read(*args)
def write(self, *args):
return self.outputfile.write(*args)
This code is untested and there may be errors in it.
Daniel Ehrenberg
__________________________________
Do you Yahoo!?
Protect your identity with Yahoo! Mail AddressGuard
http://antispam.yahoo.com/whatsnewfree
More information about the Tutor
mailing list