Python Language 2to3 tool

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Syntax

  • $ 2to3 [-options] path/to/file.py

Parameters

ParameterDescription
filename / directory_name2to3 accepts a list of files or directories which is to be transformed as its argument. The directories are recursively traversed for Python sources.
OptionOption Description
-f FIX, --fix=FIXSpecify transformations to be applied; default: all. List available transformations with --list-fixes
-j PROCESSES, --processes=PROCESSESRun 2to3 concurrently
-x NOFIX, --nofix=NOFIXExclude a transformation
-l, --list-fixesList available transformations
-p, --print-functionChange the grammar so that print() is considered a function
-v, --verboseMore verbose output
--no-diffsDo not output diffs of the refactoring
-wWrite back modified files
-n, --nobackupsDo not create backups of modified files
-o OUTPUT_DIR, --output-dir=OUTPUT_DIRPlace output files in this directory instead of overwriting input files. Requires the -n flag, as backup files are unnecessary when the input files are not modified.
-W, --write-unchanged-filesWrite output files even is no changes were required. Useful with -o so that a complete source tree is translated and copied. Implies -w.
--add-suffix=ADD_SUFFIXSpecify a string to be appended to all output filenames. Requires -n if non-empty. Ex.: --add-suffix='3' will generate .py3 files.

Remarks

The 2to3 tool is an python program which is used to convert the code written in Python 2.x to Python 3.x code. The tool reads Python 2.x source code and applies a series of fixers to transform it into valid Python 3.x code.

The 2to3 tool is available in the standard library as lib2to3 which contains a rich set of fixers that will handle almost all code. Since lib2to3 is a generic library, it is possible to write your own fixers for 2to3.



Got any Python Language Question?