views:

43

answers:

2

I have to do a very simple project in python where I add error checking to the built in file class. So far, I've got:

class RobustFile(file):
    def __init__(self,name,mode):
        file.__init__(self,name,mode)

I'm just starting out, but to make sure I hadn't messed anything up, I ran it. Well, right off the bat, I raised a NameError because it didn't recognize file. I tried tweaking it, I looked it up on the internet, I copied examples using the same format, and...all NameError. Can anyone shed some light on how exactly to subclass file?

A: 

Works fine in python 2.6.6:

In [44]: class RobustFile(file):
    def __init__(self,name,mode):
        file.__init__(self,name,mode)
   ....: 

In [47]: fp = RobustFile('foo','w')

In [48]: fp.writelines('bar')

In [49]: fp.close()
aterrel
+3  A: 

You're probably using Python 3, which no longer has a file type.

Instead, as noted in the Python 3 documentation's I/O Overview, it has a number of different stream types that are all derived from one of _io.TextIOBase, _io.BufferedIOBase, or _io.RawIOBase, which are themselves derived from _io.IOBase.

Amber
OUTSTANDING! Thank you so much! Thats so helpful! I can finally finish my project! yep! I've got it working in 2.6!
Chris