Python 2 to Python 3 code conversion tool 2to3.py

Keywords: Python Anaconda

I've been meeting the need to switch from Python 2 code to Python 3 code before. I just recently learned that Python has an official conversion tool: 2to3.py.  
Whether you download Python from the python official website or use Anaconda for installation, the directory is in {Python_HOME}\Tools\scripts. Run the 2to3.py script and print as follows:

python 2to3.py --help
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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

Suppose I have a py2.py file in the E:\pycode directory. The code is as follows:

print 'test'
  • 1

I want to change the above code to Python 3. ,
cmd should first enter the {Python_HOME}\Tools\scripts directory, and then execute the following:

python 2to3.py -w E:\pycode\py2.py
  • 1

The whole process is as follows:

D:\Anaconda3\Tools\scripts>python 2to3.py -w E:\pycode\py2.py
RefactoringTool: Skipping optional fixer: buffer
RefactoringTool: Skipping optional fixer: idioms
RefactoringTool: Skipping optional fixer: set_literal
RefactoringTool: Skipping optional fixer: ws_comma
RefactoringTool: Refactored E:\pycode\py2.py
--- E:\pycode\py2.py    (original)
+++ E:\pycode\py2.py    (refactored)
@@ -1 +1 @@
-print 'test'
+print('test')
RefactoringTool: Files that were modified:
RefactoringTool: E:\pycode\py2.py
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

Then we can see that there is an additional py2.py.bak file in the E:\pycode directory, which is the original py2.py file backup.  
View py2.py, the content has been modified to:

print('test')

Posted by kalaszabi on Thu, 02 Apr 2020 21:02:02 -0700