我正在ubuntu系统上使用python2.7。在使用“os”时,我遇到以下错误:
AttributeError:“module”对象没有属性“fsencode”
在这一行:
directory=os.fsencode(indir)
我已经检查了其他解决方案,但它们并不相关。
Python 2.7没有 os.fsencode() 函数,这是 Python 3 only function . 是的 introduced in Python 3.2 不带后端口。
os.fsencode()
如果你需要一个后置端口,你必须先后置端口 sys.getfilesystemencoding() function ,这取决于平台,您必须考虑不同Python版本处理文件名编码方式的变化(对于Windows、Python 3.6及更高版本,更改为UTF-8,旧版本使用 mbcs 编解码器,这取决于语言环境)。
sys.getfilesystemencoding()
mbcs