【问题标题】:subprocess.check_call([PYTHON_PATH, try_str[i]]) The system cannot find the specified filesubprocess.check_call([PYTHON_PATH, try_str[i]]) 系统找不到指定文件
【发布时间】:2015-09-03 18:19:00
【问题描述】:

我有一个路径 '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 并复制并粘贴每个文件夹的路径,以确保没有'\'和'\'不兼容...

请帮帮我!

谢谢。

【问题讨论】:

  • 您确定您的 python.exe 位于 PYTHON_PATH = r'C:\Python34\python.exe' 吗?

标签: python subprocess


【解决方案1】:

当你遇到类似 -

的错误时
FileNotFoundError: [WinError 2] The system cannot find the file specified

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

无论如何,您确实不需要手动将 path 的值放入 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。

【讨论】:

  • 很高兴能为您提供帮助。如果解决方案对您有用,我想请您接受答案,点击答案左侧的勾号,这将对社区有所帮助。
猜你喜欢
  • 1970-01-01
  • 2013-11-21
  • 2023-03-17
  • 2017-12-21
  • 2015-09-28
  • 2015-11-08
  • 2018-08-26
  • 2019-01-20
相关资源
最近更新 更多