[英]file not found in running shell script through python
我有一個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()
這是獲取命令並運行該命令的特定函數。 例如,當輸入為“查找”(以遞歸方式列出文件和目錄)時,輸出為:
./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
但是當輸入為“ python3 signals / scripts / script_aa00.py ”時,輸出包含一個錯誤,表示未找到script_aa00.py。 當我嘗試“ 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.