Mar-01-2019, 08:52 PM
During my recent revisiting of Python world, I realized that the pretty feature (2to3) for files conversion from Python2 to Python3 is suitable only for unixlike systems as shown in this url: https://docs.python.org/3/library/2to3.html
What happens if I'm confined to Windows operating system?
My trick is as follows. First of all, put inside any working folder this file (2to3.py)
What happens if I'm confined to Windows operating system?
My trick is as follows. First of all, put inside any working folder this file (2to3.py)
#!/usr/bin/env python from lib2to3.main import main import sys import os sys.exit(main("lib2to3.fixes"))Then, copy inside the file to be converted (example.py):
def greet(name): print("Hello, {0}!".format(name)) print("What's your name?") name = input() greet(name)Now, from console give the command:
python 2to3.py example.pyIn few seconds the new code suitable for Python3 code is formed with the same name (example.py), while the original Python2 file is automatically saved with appropriate suffix example.py.bak
# ------- example.py for Python3 ------- def greet(name): print("Hello, {0}!".format(name)) print("What's your name?") name = input() greet(name) # ------ example.py.bak, original under Python2 ----- def greet(name): print "Hello, {0}!".format(name) print "What's your name?" name = raw_input() greet(name)Finally, the option -w can be used for showing, step by step, all changes
python 2to3.py -w example.pyAll the best