2024-09-30 06:27:17 发布
网友
嘿,我正在尝试使用python运行shell脚本,使用以下行:
import subprocess shellscript = subprocess.Popen(["displaySoftware.sh"], stdin=subprocess.PIPE) shellscript.stdin.write("yes\n") shellscript.stdin.close() returncode = shellscript.wait()
但是当我运行程序时,它说找不到.sh文件
您的命令缺少“sh”,必须传递“shell=True”,并且必须对“yes\n”进行编码
您的示例代码应该如下所示:
import subprocess shellscript = subprocess.Popen(["sh displaySoftware.sh"], shell=True, stdin=subprocess.PIPE ) shellscript.stdin.write('yes\n'.encode("utf-8")) shellscript.stdin.close() returncode = shellscript.wait()
这种方法可能更好:
import subprocess shellscript = subprocess.Popen(["displaySoftware.sh"], shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE) returncode = shellscript.communicate(input='yes\n'.encode())[0] print(returncode)
在我的机器上运行此脚本时,“displaySoftware.sh”脚本(与python脚本位于同一目录中)被成功执行
您的命令缺少“sh”,必须传递“shell=True”,并且必须对“yes\n”进行编码
您的示例代码应该如下所示:
这种方法可能更好:
在我的机器上运行此脚本时,“displaySoftware.sh”脚本(与python脚本位于同一目录中)被成功执行
相关问题 更多 >
编程相关推荐