Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

430
Visualizações
Running one python script within another script using subprocess

I am working on a script to walk over a directory, and convert all the python2 files to python3. There is a utitliy (2to3.py) to acheive that. ( I am using python2.7 interpreter)

I have the following code:

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"

At the path mentioned, I have a subdirectory named "FolderA" and within that there is a simple python file having the syntax of python2 and a division operation.

I receive an error as below:

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
-------------------*************--------------------

What am I doing wrong here?

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

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

over 4 years ago · Santiago Trujillo Relatório

0

try change it to this:

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 Relatório

0

You need to combine dirpath with inputFile for the command as well. For your case, replace inputFile with file_path should fix the error since you already have file_path = os.path.join(dirpath, inputFile) defined in the code.

Also there're these issues in the code that may cause error in certain cases:

  1. some \ characters in your cmd are not escaped. To fix this you may use the raw string literals syntax, like r'''path\no\need\to\escape''';
  2. You really should use the list input format for the Popen function, otherwise the command would fail if your file path contains spaces. Popen(['/path/to/py', 'arg1', 'arg2' ...])
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda