2to3.py的初次探讨

1、安装的位置: {PYTHON安装目录}/Tools/Scripts/2to3.py

2、作用:将python2的代码转换为Python3的代码

3、使用说明:

使用python命令进行查看>python C:\Python34\Tools\Scripts\2to3.py -h

Usage:  2to3  [options]   file|dir ...

Options:

  -h, --help                 show this help message and exit

  -d, --doctests_only   Fix up doctests only

  -f FIX, --fix=FIX        Each FIX specifies a transformation; default: all

  -j PROCESSES, --processes=PROCESSES      Run 2to3 concurrently

  -x NOFIX, --nofix=NOFIX   Prevent a transformation from being run

  -l, --list-fixes            List available transformations

  -p, --print-function  Modify the grammar so that print() is a function

  -v, --verbose            More verbose logging

  --no-diffs                 Don't show diffs of the refactoring

  -w, --write                Write back modified files

  -n, --nobackups       Don't write backups for modified files

  -o OUTPUT_DIR, --output-dir=OUTPUT_DIR

                        Put output files in this directory instead of

                        overwriting the input files.  Requires -n.

  -W, --write-unchanged-files

                        Also write files even if no changes were required

                        (useful with --output-dir); implies -w.

  --add-suffix=ADD_SUFFIX

                        Append this string to all output filenames. Requires

                        -n if non-empty.  ex: --add-suffix='3' will generate

                        .py3 files.


例子:

在D盘新建了一个python2.7的代码,p2.py:

print "Hello,World!"
raw_input()

使用2to3.pyh进行转换,进入源代码目录,D:\>python C:\Python34\Tools\Scripts\2to3.py -w p2.py

print ("Hello,World!")
input()

转换后,源目录中出现的两个文件:

转化后的p2.py以及转化之前的p2.py.bak


总结:2to3.py将python2的代码自动转化为Python3的代码

你可能感兴趣的:(2to3.py的初次探讨)