Search code examples
pythonpython-2.7python-os

Getting error regarding "os" module of python


I am using python2.7 on ubuntu system. I am getting following errors while working with "os":

AttributeError: 'module' object has no attribute 'fsencode'

On this line:

 directory=os.fsencode(indir)

I have checked other solutions but they are not relevant.


Solution

  • Python 2.7 has no os.fsencode() function, that's a Python 3 only function. It was introduced in Python 3.2 and not backported.

    If you need a backport, you'll have to first backport the sys.getfilesystemencoding() function, which is platform dependent, and you'd have to take into account changes to how different Python versions have handled filename encoding (for Windows, Python 3.6 and up changed to UTF-8, older versions use the mbcs codec, which is locale dependent).