Forrest logo
back to the 2to3 tool

2to3:tldr:08203

2to3: Convert a Python 2 file to Python 3.
$ 2to3 --write ${filename-py}
try on your machine

The command "2to3 --write ${filename-py}" is used to convert a Python 2 code file to Python 3 using the "2to3" tool.

Here is the breakdown of the command:

  • "2to3" is the command-line tool provided by Python to automate the conversion process from Python 2 to Python 3. It analyzes the code and performs the necessary changes to make it Python 3 compatible.
  • "--write" is an option that instructs the tool to overwrite the original file with the converted code. Without this option, the conversion suggestions are displayed on the terminal without modifying the file.
  • "${filename-py}" is a placeholder that represents the name of the Python 2 file you want to convert. You need to replace it with the actual filename or path to the file you want to convert.

For example, if you have a Python 2 file named "example.py", you would run the command as: "2to3 --write example.py". This will convert the code in "example.py" to Python 3 and overwrite the original file with the converted version.

This explanation was created by an AI. In most cases those are correct. But please always be careful and never run a command you are not sure if it is safe.
back to the 2to3 tool