Skip to content Skip to sidebar Skip to footer

How Do I Override File.write() In Python 3?

The code below works on Python 2.6 but not on Python 3.x: old_file_write = file.write class file(): def write(self, d): if isinstance(d, types.bytes): sel

Solution 1:

I see two problems.

1: Your file class isn't inheriting from any specific class. If I've interpreted the situation correctly, it should be a subclass of io.TextIOWrapper.

2: In both Python 2.6 and 3.x, the types module (which would need to be imported in the first place) has no element bytes. The recommended method is to just use bytes on its own.

Redone snippet:

import io, sys

class file(io.TextIOWrapper):
    def write(self, d, encoding=sys.getdefaultencoding()):
        if isinstance(d, bytes):
            d = d.decode(encoding)
        super().write(d)

old_stdout = sys.stdout    # In case you want to switch back to it again

sys.stdout = file(open(output_file_path, 'w').detach())  # You could also use 'a', 'a+', 'w+', 'r+', etc.

Now it should do what you want it to, using sys.stdout.write to the output file that you specify. (If you don't wish to write to a file on disk but instead wish to write to the default sys.stdout buffer, using sys.stdout = file(sys.stdout.detach()) would probably work.)

Do note that, since Python 3.x does not have the file class, but 2.6 does have the io module, you will have to use one of the classes of the io module. My above code is just an example, and if you want it to be more flexible you'll have to work that out on your own. That is, depending on what sort of file you're writing to/what mode you're writing to in, you'll probably want to use a different class in io.


Solution 2:

old_file_write = file.write 

You're using the class-level method of the file class.

old_file_write(d)

Should never have worked. I believe you're still copying and pasting incorrectly.

I think you may have had

old_file_write(self,d)

Which might have worked.

Your approach is not very good. Think about this instead.

class MyKindOfFile( file ):
    def write(self, d):
        if isinstance(d, types.bytes):
            self.buffer.write(d)
        else:
            super( MyFindOfFile, write )(d)

This will work out a LOT better for you, since it uses simple inheritance in a more typical way.


Solution 3:

file objects can write bytes, but you need to open the file in the correct mode

fp = open('file.bin', 'wb') # open in binary mode
fp.write(b"some bytes") 
pf.close()

If you want to write strings to the disk you need to encode them first.


Post a Comment for "How Do I Override File.write() In Python 3?"