0

I am new to python. I am trying to port a python 2 file to python 3. I understand that "file" is not a type in python 3, unlike in python 2. The class in my python 2 file is defined with a file object. I tried but could not find a way to port this to python 3.

class MyFile(file):
  def __init__(self, fname):
    file.__init__(self, fname, *args, **kwargs)

This class was used in the main module as follow:

fname = "myfile.bin"
f = MyFile(fname)

The above worked in python 2 but cannot work in python 3 because "file" is undefined. I know this looks simple to fix but I really cannot get it to work in python 3. Please help.

Meil
  • 1
  • 1
  • 2
    What are you trying to implement with this? Your example does not require subclassing as it is. – Klaus D. Aug 08 '20 at 04:36
  • 2
    https://docs.python.org/3/library/io.html#io.IOBase? – Iain Shelvington Aug 08 '20 at 04:38
  • Hi Klaus! I am trying to custom read a binary file (written using fortran). I understand that there may be a scipy.io.FortranFile function. Unfortunately, I am working with an old code written in python 2 and am trying to port it over to python 3. – Meil Aug 08 '20 at 05:06
  • @Meil sometimes it might be easier to work with an object as an *attribute* instead of subclassing it, perhaps that can be the case here too – M Z Aug 16 '20 at 05:12

0 Answers0