0

I have a folder of .py files written in Python 2.7 that I want to convert to Python 3 using the 2to3 tool. Using windows 10 in the cmd prompt i can convert a single file with the following command:

C:\Users\t\Desktop\search>python.exe 2to3.py -w graphicsDisplay.py

however this line is not syntactically correct when in python shell and ideally I'd like to be able to iterate through the whole folder and update all .py files using the by using the following python code in cmd:

C:\Users\t\Desktop\search>python
>>> import os
>>> for files in os.listdir('*filepath*'):
>>>        if '.py' == str(files[-3:]):
>>>            *...some line of code here to perform 2to3*

its the last line which I can't seem to get right so I guess my question is, how can I call the 2to3 function in python on each iteration of the files variable?

Braide
  • 155
  • 3
  • 3
  • 13

2 Answers2

2

You can do it directly from command line

for %a in (*.py) do python.exe 2to3.py -w "%a"

For each file in the indicated set execute the conversion passing the for replaceable parameter (%a in this sample) that holds the reference to the file being iterated.

MC ND
  • 69,615
  • 8
  • 84
  • 126
0

Looks like 2to3 support recursive folder checking if you leave out an explicit script to convert.

Would it be easier to have all your scripts in one folder and execute against that instead?

from: https://docs.python.org/2/library/2to3.html#

2to3 --output-dir=python3-version/mycode -W -n python2-version/mycode
Ezsrac
  • 1
  • 3
  • Ezsrac thank you for your answer. Another worked perfectly so I haven't tried this but I appreciate you responding. – Braide Feb 16 '17 at 19:02