简体   繁体   English

通过python在运行shell脚本中找不到文件

[英]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: 我有一个python脚本,在代码的单个部分中,必须执行一个终端命令,这是代码:

# 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. 但是当输入为“ python3 signals / scripts / script_aa00.py ”时,输出包含一个错误,表示未找到script_aa00.py。 when i try " python3 signals/scripts/script_aa00.py " myself in terminal (in this dir) works fine 当我尝试“ python3 signals / scripts / script_aa00.py ”时,我自己在终端(在此目录中)工作正常

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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