Is it possible to run a python script that is in another directory?

Asked

Viewed 346 times

0

I have a python script "dad":

# Executar script de outra pasta
if __name__ == '__main__':
     cmd = "main.py -username abcd -password 1234"
     subprocess.call(cmd, Shell=True)

Which calls the script "son" main.py (which is in another directory with all its modules and subfolders), also with the two parameters, as example above.

def main(argv):
    global username = "" #argv
    global password = "" #argv
    acessa_site_com_login #criar csv na pasta para cada texto acessado, demora uns minutos para finalizar

if __name__ == "__main__":
    main(sys.argv[1:])

Then I have in folder 1 the "father" and folder 2 with the "son".

I cannot find a way to call/run the script, it being in another directory. All the information I found here on the site shows imports to files that are in the same folder and/or to call functions from the "child" module, but I want to run the script as if I were running it at the command prompt. When I open the cmd prompt and type python main.py -u ABDC -p 1234 works normal.

2 answers

1


To run the script . py you need to call the interpreter python

import subprocess

if __name__ == '__main__':
    cmd = r"python /caminho/para/a/pasta/main.py -username abcd -password 1234"
    subprocess.call(cmd, shell=True)

1

import os

if(os.name == "nt"):
    os.system("python3 covid19/san_covid19(Windows).py")
else:
    os.system("python3 covid19/'san_covid19(linux).py'")

Example of an algorithm, works in the directory windows and linux. Importing them makes it run on both operating systems

Browser other questions tagged

You are not signed in. Login or sign up in order to post.