Linux服务器上执行Python脚本打印中文路径报错:
print(txtpath)
UnicodeEncodeError: 'utf-8' codec can't encode characters in position 59-65: surrogates not allowed
使用replace可以消除报错,但中文路径无法正常显示:
print(txtpath.encode('utf-8', 'replace').decode())
根本原因在于中文命名文件从Windows上通过Xftp等工具传输至Linux时编码格式不匹配,此时Linux路径下的中文已经变成了乱码:
解决方案:将多个文件打包为zip压缩包传输,在Linux上解压:
unzip xxx.zip