Linux command
2to3 命令
网络
复制后可按需替换文件名、目录或参数。
常用示例
Show changes without applying
2to3 [script.py]
Convert file in place
2to3 -w [script.py]
Convert entire directory
2to3 -w [directory/]
Apply specific fixers only
2to3 -f [print] -f [import] [script.py]
List available fixers
2to3 -l
Convert in place without creating .bak files
2to3 -w -n [script.py]
Write converted files to a different directory
2to3 -w -n -o [out_dir/] [script.py]
说明
2to3 is an automated Python 2 to Python 3 code translator. It parses Python 2 source code and applies "fixers" that transform incompatible syntax and library calls to their Python 3 equivalents. Common transformations include: - print statement to print() function - unicode to str, str to bytes - raw_input() to input() - Iterator methods (.iteritems() to .items()) - except Exception, e to except Exception as e - Library renames (urllib, ConfigParser, etc.) 2to3 handles most mechanical changes but may require manual review for semantic differences between Python 2 and 3.
参数
- -w, --write
- Write changes to files
- -n, --nobackups
- Don't create .bak backup files
- -f _fixer_, --fix _fixer_
- Apply specific fixer
- -x _fixer_, --nofix _fixer_
- Exclude specific fixer
- -l, --list-fixes
- List available fixers
- -p, --print-function
- Treat print as a function (skip the print fixer). Useful when the file already uses from __future__ import print_function.
- -d, --doctests_only
- Only fix doctests inside the file.
- -W, --write-unchanged-files
- Write output even for unchanged files (useful with -o).
- --add-suffix _SUFFIX_
- Append this suffix to output filenames instead of overwriting.
- -j _n_, --processes _n_
- Run in parallel with n processes
- -o _dir_, --output-dir _dir_
- Write converted files to directory
- -v, --verbose
- More verbose output
FAQ
What is the 2to3 command used for?
2to3 is an automated Python 2 to Python 3 code translator. It parses Python 2 source code and applies "fixers" that transform incompatible syntax and library calls to their Python 3 equivalents. Common transformations include: - print statement to print() function - unicode to str, str to bytes - raw_input() to input() - Iterator methods (.iteritems() to .items()) - except Exception, e to except Exception as e - Library renames (urllib, ConfigParser, etc.) 2to3 handles most mechanical changes but may require manual review for semantic differences between Python 2 and 3.
How do I run a basic 2to3 example?
Run `2to3 [script.py]` in a terminal, then adjust file names, paths, flags, or remote targets for your system.
What does -w, --write do in 2to3?
Write changes to files