The Wayback Machine - https://web.archive.org/web/20240118172952/https://www.geeksforgeeks.org/automate-the-conversion-from-python2-to-python3/
Open In App
Related Articles

Automate the Conversion from Python2 to Python3

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

We can convert Python2 scripts to Python3 scripts by using 2to3 module. It changes Python2 syntax to Python3 syntax. We can change all the files in a particular folder from python2 to python3.

Installation

This module does not come built-in with Python. To install this type the below command in the terminal.

pip install 2to3

Syntax:

2to3 [file or folder] -w

If we want to change all files in the currently open folder and all the files in the subfolder from Python2 to Python3 type the below command.

2to3.-w

If we want to change a particular file in the current folder from Python2 to Python3 then enter the following command.

2to3 gfg.py -w

Example: Consider a simple Python2 file.

python-2to3

To convert this file from Python2 to Python3 open the terminal in the directory containing the file and type the below command.

python2to3

The Python file will now be converted to Python3. Let’s see the file.

Python-2to3

Don't miss your chance to ride the wave of the data revolution! Every industry is scaling new heights by tapping into the power of data. Sharpen your skills, become a part of the hottest trend in the 21st century.
Dive into the future of technology - explore the Complete Machine Learning and Data Science Program by GeeksforGeeks and stay ahead of the curve.

Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated : 16 Jul, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads
Complete Tutorials