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

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

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

cd C:UsersAdministratorAppDataLocalProgramsPythonPython37Toolsscripts

python 2to3.py -w C:UsersAdministratorPycharmProjectsDataDrivenFrameWorkutilObjectMap.py

python 2to3.py C:UsersAdministratorDesktopocrserver.py

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

python 2to3.py -w C:UsersAdministratorDesktopocrserver.py

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

-f 给出明确的修复集


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

原文地址:https://www.cnblogs.com/emanlee/p/12398190.html