subprocess.Popen 在同一目录中找不到 bash 文件

问题描述 投票:0回答:1

我正在尝试从 Python 脚本运行 bash 脚本。该 bash 脚本和 python 脚本位于同一目录中。但是,当我运行 Python 脚本时,出现

No such file or directory
错误。

我当前的目录

xxx@xxx:/home# ls
check.sh sc.py 

以下是我尝试运行的代码,

sc.py

import subprocess
subprocess.Popen(["check.sh", "ABC"])

我收到以下错误:

xxx@xx:/home# python3 sc.py
Traceback (most recent call last):
  File "sc.py", line 2, in <module>
    subprocess.Popen(["check.sh", "ABC"])
  File "/usr/lib/python3.6/subprocess.py", line 729, in __init__
    restore_signals, start_new_session)
  File "/usr/lib/python3.6/subprocess.py", line 1364, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'check.sh': 'check.sh'

我在这里做错了什么?

python shell subprocess
1个回答
0
投票

PATH 中没有文件 check.sh 。如果您想在 current 工作目录中查找某些内容,请在那里找到它。

subprocess.Popen(["./check.sh", "ABC"])

如果您想在与 python 脚本相同的目录中查找某些内容,请导航到与 python 脚本相同的目录。 import subprocess from pathlib import Path subprocess.Popen([str(Path(__file__).parent / "check.sh"), "ABC"])

© www.soinside.com 2019 - 2024. All rights reserved.