


Do you have a practical method to complete the conversion between Python3 and Python2 scripts in one second?
There are some differences between Python2 and Python3 in the syntax and use of modules (not explained in detail here). It is recommended that novices directly use python3 for code writing. In actual work, there are many operation and maintenance or test scripts that are still running continuously using python2. When we encounter batch work that needs to convert python3 into python2 (or convert python2 into python3), how should we deal with it? Do we need a line? How about manually modifying the code line by line? The answer is no. This article will introduce the specific implementation plan.
Convert python 2 to python 3
Python3.7 (comes with the conversion tool C: Python37Toolsscripts2to3) We can use it directly in the cmd command line:
python 2to3.py -w D:/test.py #Python2's test.py is converted to python3
The test.py file is very simple, just print the statement
print "hello.py"
C:Python37Toolsscripts>python 2to3.py -w D:/test.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 D:/test.py --- D:/test.py (original) +++ D:/test.py (refactored) @@ -1 +1 @@ -print "hello.py" +print("hello.py") RefactoringTool: Files that were modified: RefactoringTool: D:/test.py
After execution, check test.py, converted The code is as follows:
print("hello.py")
The -w parameter will overwrite the old file with the new file. Without -w, only the modified places will be displayed in the console window (and the content in the file will not be modified); the file will be Back up to .bak (for example, test.py.bak). If you do not need to generate a bak file, just add the parameter -n.
If you need to convert all the files in a certain folder, such as all the files in the test folder on drive D, enter in the command line:
python 2to3.py -w D:/test/
python 3 to python 2
First install a Python package: lib3to2, pip install 3to2
After successful installation, a file called 3to2 will be generated in the directory C:Python37Scripts
for a certain python file that needs to be converted , for example, test.py in the root directory of drive D, enter in the command line:
python 3to2 -w D:/test.py #python3 test.py is converted to python2
if You need to convert all the files in a certain folder, such as all the files in the test folder on drive D, enter in the command line:
python 3to2 -w D:/test/
You can find the method of converting python 3 to python 2 and the method of converting python 2 to python 3 are extremely similar!
The above is the detailed content of Do you have a practical method to complete the conversion between Python3 and Python2 scripts in one second?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



How PHP8 improves performance by writing code Summary: With the release of PHP8, many developers want better performance in their applications. This article will explore some tips for writing efficient code to improve PHP8 performance. Introduction: In today's web applications, performance is very important. Users expect fast-loading pages and responsive interactions. PHP8 is a powerful language that can be used to build high-performance applications. However, you can only get the most out of P if you use the right coding techniques

In Python 2.x versions, pip may not install automatically. The manual installation steps: 1. Make sure Python 2.x is installed; 2. Download the get-pip.py script. Run the "wget https://bootstrap.pypa.io/get-pip.py" command in the terminal to download the script; 3. Run the "python get-pip.py" command to install pip.

Practical Guide to PyCharm Environment Configuration: Make Your Code Writing Easier With the widespread application of the Python language, more and more developers choose to use PyCharm as their integrated development environment (IDE). PyCharm has powerful functions and rich plug-ins that can help developers improve work efficiency, but before using PyCharm, we need to configure it to ensure that it can fully utilize its potential. This article will introduce how to configure the environment of PyCharm so that the code can be compiled

How to gain an in-depth understanding of PHP8 performance optimization techniques by writing code. Introduction With the development of the Internet, website performance optimization has become more and more important. For websites developed using the PHP language, PHP8 is the latest version, bringing many new features for performance optimization. This article will introduce how to gain an in-depth understanding of PHP8 performance optimization techniques by writing code. 1. Use the JIT compiler to improve performance. PHP8 introduces the Just-In-Time (JIT) compiler, which can directly compile PHP code into machine code.

How to use PHP to write the daily inventory counting function code in the inventory management system 1. Introduction Having accurate inventory data is very important for any enterprise. Inventory counting is one of the important steps in maintaining inventory accuracy. Through daily inventory, inventory data can be verified, potential problems can be discovered, and inventory records can be adjusted in a timely manner to ensure the accuracy of inventory data. This article will introduce how to use PHP to write the daily inventory function code in a simple inventory management system. 2. Database Design Before starting to write code, we first

Comprehensive application of PHP, Unity3D and Workerman: How to create a new virtual world Since its inception, virtual reality (VirtualReality) technology has attracted widespread attention and enthusiastic pursuit. Virtual reality technology enables users to experience an immersive experience similar to the real world through a computer-generated virtual environment. In this article, we will explore how to use the comprehensive application of PHP, Unity3D and Workerman to create a

Installation steps: 1. Make sure that Python3 has been installed and can be accessed through the command line; 2. Open the terminal and enter the "python3 -m ensurepip --upgrade" command to install pip; 3. Download the pip installation package from the official Python website; 4. Extract the downloaded pip installation package into a directory; 5. Open the terminal and navigate to the decompressed pip directory; 6. Run the "python3 setup.py install" command to install pip.

When beginners write Python code, they often encounter some string processing problems, such as parsing data from an HTML web page, extracting specific information from a text file, or intercepting key parts from a long text, etc. At this time, we can use regular expressions to solve these problems. This article will introduce how to use Python's regular expressions to write code and improve coding efficiency. 1. What is a regular expression? Regular expressions are a method for matching strings using special symbols and words.
