Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/212.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 2to3脚本不工作-Unicode错误_Python_Python 2to3 - Fatal编程技术网

Python 2to3脚本不工作-Unicode错误

Python 2to3脚本不工作-Unicode错误,python,python-2to3,Python,Python 2to3,我发现了一个包含大量Python2文件的repo,其中包含一个将它们转换为python3的脚本。但是,当我运行它时,会出现以下错误: SyntaxError:(unicode错误)'UnicodeScape'编解码器无法解码位置3-4的字节:截断\uxxxxx转义 我所做的唯一更改是将路径添加到2to3,而不是只使用2to3,因为这不在我的路径中 有什么建议可以让它工作吗 import os def makepython3(): """This is a script to trans

我发现了一个包含大量Python2文件的repo,其中包含一个将它们转换为python3的脚本。但是,当我运行它时,会出现以下错误:

SyntaxError:(unicode错误)'UnicodeScape'编解码器无法解码位置3-4的字节:截断\uxxxxx转义

我所做的唯一更改是将路径添加到
2to3
,而不是只使用
2to3
,因为这不在我的路径中

有什么建议可以让它工作吗

import os

def makepython3():
    """This is a script to transform all the solutions into 
    Python 3 solutions."""
    files = os.listdir('exercises')

    exfolder = 'exercises'
    ex3folder = 'exercisespy3'

    if not os.path.exists(ex3folder):
        os.mkdir(ex3folder)

    for f in files:
        os.system('cp {} {}'.format(exfolder+os.sep+f, ex3folder+os.sep+f))
        if f.endswith('.py'):
            os.system('"C:\Users\HP\AppData\Local\Programs\Python\Python37-32\Tools\scripts\2to3.py" -w -n --no-diffs {}'.format(ex3folder+os.sep+f))

    print('All done!')

if __name__ == '__main__':
    makepython3()
问题在于:

os.system('"C:\Users\HP\....
              ^-- interpreted as a \U unicode escape
尝试使用原始字符串:

os.system(r'"C:\Users\HP\....

\U
转义序列是在Python3中引入的,这解释了脚本在Python2中工作的原因。但是,在处理文本windows路径时,应始终使用原始字符串。

只需将
os.path.sep
os.path.join
一起使用即可构建路径,而不是硬编码字符串

从os.path导入连接,sep
windows_exe_path=加入(
九月,
“C:”+9月,
“用户”,年月日,
“惠普”,9月,
“AppData”,2009年9月,
“本地”,9月,
“计划”,9月,
“Python”,9月
“蟒蛇37-32”,9月,
“工具”,2001年9月,
“脚本”,9月,
“2to3.py”,
)
否则,
/
也将起作用

windows\u exe\u path=“C:/Users/HP/AppData/Local/Programs/Python/Python37-32/Tools/scripts/2to3.py”