简体   繁体   中英

file not found in running shell script through python

I have a python script, in a single part of code, have to execute a terminal command this is code:

# run shell script and return response printed
def run_shell(command_string):
    p = subprocess.Popen([command_string], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    return p.communicate()

it's the certain function that gets a command and runs that. for example when input is "find" (to list files and dirs recursively) output is:

./actions
./actions/scripts
./actions/scripts/script_aa00.py
./log_list_template.json
./README.md
./.git
./main
./main/Classifier.py
./main/Solver.py
./main/learner.py
./main/Database.py
./main/__pycache__
./main/__pycache__/Database.cpython-35.pyc
./main/__pycache__/learner.cpython-35.pyc
./start.sh
./run.py
./signals
./signals/scripts
./signals/scripts/script_aa00.py  [I want to run this script]
./signals/list0.json
./signals/template.json

but when input is " python3 signals/scripts/script_aa00.py " output contains an error that means script_aa00.py not found. when i try " python3 signals/scripts/script_aa00.py " myself in terminal (in this dir) works fine

您可以尝试将shell=true添加到Popen和/或使用绝对路径:

p = subprocess.Popen(['/usr/bin/python3', '/home/.../signals/scripts/script_aa00.py'], shell=true, stdout=subprocess.PIPE, ...)

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