简体   繁体   中英

Run terminal script in python?

I am using python and I am trying to run a shell script that is located in another folder I am trying

subprocess.call(['source','../Apps/appName/run'])

Where 'run' is a shell script I wrote and made an executable. But it keeps giving errors such as

No such file or directory or **No such file or directory: "source"

I have also tried the following

subprocess.call(['source','../Apps/appName/run']) subprocess.call(['source ../Apps/appName/run']) subprocess.call(['.','../Apps/appName/run'])

I am trying to run the script and leave it alone (as in I do not want any return value or to see what the output of the shell script is.

Thank you in advance

source is a shell builtin that reads a file and interprets the commands in the current shell instance. It's similar to C #include or Python import . You should not be using it to run shell scripts.

The correct way to run a script is to add a shebang like #!/bin/bash to the first line, and chmod +x yourscriptfile . The OS will then consider it a real executable, which can be executed robustly from any context. In Python, you would do:

subprocess.call(['../Apps/appName/run'])

If for whichever reason this is not an option, you can instead explicitly invoke bash on the file, since this is similar to what would happen if you're in bash and type source :

subprocess.call(['bash', '../Apps/appName/run'])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM