使用python内置2to3工具将python2代码转换为python3代码

我们都知道python有一个一直被诟病的毛病,python2与python3代码不兼容问题,而网上的一些教学大部分都是python2的,如果需要将其在python3环境下运行,有两个方法,一是:一个一个参考文档,对其中的库在3中的修改对应修改;二则是使用python内置工具2to3.py对代码进行批量修改。

首先找到2to3.py文件位置,一般是在“\Python\Python36-32\Tools\scripts”这样的文件层内,具体要看自己python安装的位置。

cd C:\Users\Administrator\AppData\Local\Programs\Python\Python37\Tools\scripts

python 2to3.py -w C:\Users\Administrator\PycharmProjects\DataDrivenFrameWork\util\ObjectMap.py

python 2to3.py C:\Users\Administrator\Desktop\ocr\server.py

接-w加文件可以将修改的文件覆盖到原文件并留有.bak的备份文件用来恢复

python 2to3.py -w C:\Users\Administrator\Desktop\ocr\server.py

-w -n效果是修改但不留备份文件

-f 给出明确的修复集


原文链接:https://blog.csdn.net/qq_36275540/article/details/80389941

猜你喜欢

转载自www.cnblogs.com/emanlee/p/12398190.html