2017-01-14 113 views
0

發現我有一個python腳本,在一個單一的代碼部分,必須執行一個終端的命令,這是代碼:文件不是在運行shell腳本通過蟒蛇

# 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信號/腳本/ script_aa00.py」輸出包含這意味着script_aa00錯誤。 py找不到。當我嘗試「python3信號/腳本/ script_aa00.py」自己在終端(在此DIR)工作正常

回答

0

你可以嘗試添加shell=true到POPEN和/或使用絕對路徑:

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

tnx工作得很好:) – reza