Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

427
Views
Ejecutar un script de python dentro de otro script usando un subproceso

Estoy trabajando en un script para recorrer un directorio y convertir todos los archivos python2 a python3. Hay una utilidad (2to3.py) para lograrlo. (Estoy usando el intérprete python2.7)

Tengo el siguiente código:

 import os import subprocess import pathlib APP_FOLDER = 'C:/Users/XXXX/Test/' for dirpath, dirnames, filenames in os.walk(APP_FOLDER): for inputFile in filenames: if pathlib.Path(inputFile).suffix == ".py": file_path = os.path.join(dirpath, inputFile) with open(file_path) as f: num_lines = len(f.readlines()) print dirpath print inputFile print "lines of code: ", num_lines print "converting to python 3" cmd ="py C:\Python27\Tools\Scripts\\2to3.py "+inputFile+" -w" p1=subprocess.Popen(cmd,shell=True) p1.wait() if p1.returncode ==0: print "Success" else: print "failure"

En la ruta mencionada, tengo un subdirectorio llamado "FolderA" y dentro hay un archivo python simple que tiene la sintaxis de python2 y una operación de división.

Recibo un error como el siguiente:

 C:/Users/XXXX/Test/FolderA Sample.py lines of code: 7 converting to python 3 RefactoringTool: Skipping optional fixer: buffer RefactoringTool: Skipping optional fixer: idioms RefactoringTool: Skipping optional fixer: set_literal RefactoringTool: Skipping optional fixer: ws_comma RefactoringTool: Can't open Sample.py: [Errno 2] No such file or directory: 'Sample.py' RefactoringTool: No files need to be modified. RefactoringTool: There was 1 error: RefactoringTool: Can't open Sample.py: [Errno 2] No such file or directory: 'Sample.py' failure -------------------*************--------------------

¿Qué estoy haciendo mal aquí?

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Intente usar, cmd ="py C:\Python27\Tools\Scripts\\2to3.py "+file_path+" -w"

over 4 years ago · Santiago Trujillo Report

0

intenta cambiarlo a esto:

 import os import subprocess import pathlib APP_FOLDER = 'C:/Users/XXXX/Test/' for dirpath, dirnames, filenames in os.walk(APP_FOLDER): for inputFile in filenames: if pathlib.Path(inputFile).suffix == ".py": file_path = os.path.join(dirpath, inputFile) with open(file_path) as f: num_lines = len(f.readlines()) print dirpath print inputFile print "lines of code: ", num_lines print "converting to python 3" cmd = "py C:\Python27\Tools\Scripts\\2to3.py "+file_path+" -w" p1=subprocess.Popen(cmd,shell=True) p1.wait() if p1.returncode ==0: print "Success" else: print "failure"
over 4 years ago · Santiago Trujillo Report

0

También debe combinar dirpath con inputFile para el comando. Para su caso, reemplace inputFile con file_path debería corregir el error ya que ya tiene file_path = os.path.join(dirpath, inputFile) definido en el código.

También hay estos problemas en el código que pueden causar errores en ciertos casos:

  1. algunos \ caracteres en su cmd no se escapan. Para solucionar esto, puede usar la sintaxis de literales de cadena sin procesar, como r'''path\no\need\to\escape''' ;
  2. Realmente debería usar el formato de entrada de list para la función Popen , de lo contrario, el comando fallaría si la ruta de su archivo contiene espacios. Popen(['/path/to/py', 'arg1', 'arg2' ...])
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!