python - subprocess.check_call([PYTHON_PATH, try_str[i]]) 系统找不到指定文件

标签 python subprocess

我有一个路径“D:\Torres\Gas_Entrapment\new_calculations\command_script_load\Es_cteS_cte\w_load”,其中存储了 9(九)个文件夹。

每个文件夹都包含一个 main.py 以及其他内容。

我编写了一个脚本并将其放在目录:'D:\Torres\Gas_Entrapment\new_calculations\command_script_load\Es_cteS_cte\w_load',以便它将访问每个文件夹中包含的 main.py。

这是我的代码:

import subprocess 
import os  

PYTHON_PATH = r'C:\Python34\python.exe' 
CURRENT_PATH = r'D:\Torres\Gas_Entrapment\new_calculations\command_script_load\Es_cteS_cte\w_load'

try_str = [r'D:\Torres\Gas_Entrapment\new_calculations\command_script_load\Es_cteS_cte\w_load\1\main.py',\ 
r'D:\Torres\Gas_Entrapment\new_calculations\command_script_load\Es_cteS_cte\w_load\2\main.py',\
r'D:\Torres\Gas_Entrapment\new_calculations\command_script_load\Es_cteS_cte\w_load\3\main.py',\
r'D:\Torres\Gas_Entrapment\new_calculations\command_script_load\Es_cteS_cte\w_load\4\main.py',\
r'D:\Torres\Gas_Entrapment\new_calculations\command_script_load\Es_cteS_cte\w_load\5\main.py',\
r'D:\Torres\Gas_Entrapment\new_calculations\command_script_load\Es_cteS_cte\w_load\6\main.py',\ 
r'D:\Torres\Gas_Entrapment\new_calculations\command_script_load\Es_cteS_cte\w_load\7\main.py',\ 
r'D:\Torres\Gas_Entrapment\new_calculations\command_script_load\Es_cteS_cte\w_load\8\main.py',\ 
r'D:\Torres\Gas_Entrapment\new_calculations\command_script_load\Es_cteS_cte\w_load\9\main.py']   

for i in range(len(try_str)):   
   subprocess.check_call([PYTHON_PATH, try_str[i]])

这是我执行时遇到的异常

D:\Torres\Gas_Entrapment\new_calculations\command_script_load\Es_cteS_cte\w_load>python subprocesses_handler.py

D:\Torres\Gas_Entrapment\new_calculations\command_script_load\Es_cteS_cte\w_load\1\main.py True

Traceback (most recent call last):
  File "subprocesses_handler.py", line 33, in <module>
    subprocess.check_call([PYTHON_PATH, try_str[i]])
  File "C:\Users\torresl\AppData\Local\Continuum\Anaconda3 \lib\subprocess.py", line 556, in check_call
retcode = call(*popenargs, **kwargs)
  File "C:\Users\torresl\AppData\Local\Continuum\Anaconda3\lib\subprocess.py", line 537, in call
with Popen(*popenargs, **kwargs) as p:
  File "C:\Users\torresl\AppData\Local\Continuum\Anaconda3\lib\subprocess.py", line 859, in __init__
restore_signals, start_new_session)
  File "C:\Users\torresl\AppData\Local\Continuum\Anaconda3\lib\subprocess.py", line 1112, in _execute_child
startupinfo)
FileNotFoundError: [WinError 2] Das System kann die angegebene Datei nicht finden

“Das System kann die angegebene Datei nicht finden”是德语,意思是“系统找不到文件”

<小时/>

此时我真的不知道发生了什么...首先我使用模块 os 制作了一个列表以获取目录中的所有文件夹...然后我制作了列表 try_str 并复制并粘贴每个文件夹的路径,以确保“\”和“\”不兼容...

请帮帮我!

谢谢。

最佳答案

当您收到类似 - 的错误时

FileNotFoundError: [WinError 2] The system cannot find the file specified

或其德语版本,这意味着您指定了错误的可执行文件。我的猜测是 Python 可执行文件不在您指定的位置 - C:\Python34\python.exe

无论如何,您确实不需要手动将路径值放入 Python.exe ,您可以改为使用 sys.executable获取运行当前程序的 Python 可执行文件的路径。

您可以在您的程序中使用它。示例-

import subprocess 
import os  
import sys

CURRENT_PATH = r'D:\Torres\Gas_Entrapment\new_calculations\command_script_load\Es_cteS_cte\w_load'

try_str = [r'D:\Torres\Gas_Entrapment\new_calculations\command_script_load\Es_cteS_cte\w_load\1\main.py',\ 
r'D:\Torres\Gas_Entrapment\new_calculations\command_script_load\Es_cteS_cte\w_load\2\main.py',\
r'D:\Torres\Gas_Entrapment\new_calculations\command_script_load\Es_cteS_cte\w_load\3\main.py',\
r'D:\Torres\Gas_Entrapment\new_calculations\command_script_load\Es_cteS_cte\w_load\4\main.py',\
r'D:\Torres\Gas_Entrapment\new_calculations\command_script_load\Es_cteS_cte\w_load\5\main.py',\
r'D:\Torres\Gas_Entrapment\new_calculations\command_script_load\Es_cteS_cte\w_load\6\main.py',\ 
r'D:\Torres\Gas_Entrapment\new_calculations\command_script_load\Es_cteS_cte\w_load\7\main.py',\ 
r'D:\Torres\Gas_Entrapment\new_calculations\command_script_load\Es_cteS_cte\w_load\8\main.py',\ 
r'D:\Torres\Gas_Entrapment\new_calculations\command_script_load\Es_cteS_cte\w_load\9\main.py']   

for i in range(len(try_str)):   
   subprocess.check_call([sys.executable, try_str[i]])

尽管在大多数情况下,您实际上甚至不需要这个,您可以简单地使用 'python' ,并让操作系统根据 PATH 环境变量。

关于python - subprocess.check_call([PYTHON_PATH, try_str[i]]) 系统找不到指定文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32372657/

相关文章:

python - 解释sklearn的scale()和乘以STD并加上平均值之间的区别

python - 在 GitPython 中获取第一次提交的差异细节

subprocess - 带 SBCL 的二进制管道

python - 如何打开可见的全尺寸 Excel?

python struct pack 加速函数内部

python - 提取 Pandas 中日期时间类型列的第一天

python - 如何确定给定的 RGB 颜色是否介于一系列已知颜色之间?

python - 在 python 中替代子进程

python - 处理 Windows 中的子进程崩溃

python - 如何执行我的脚本并在 PYQT5 的 GUI 中打印它?