WinError 2 系统找不到指定的文件 (Python)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42572582/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 21:54:28  来源:igfitidea点击:

WinError 2 The system cannot find the file specified (Python)

pythonpython-2.7python-3.xbioinformaticsf2py

提问by Jone

I have a Fortran program and want to execute it in python for multiple files. I have 2000 input files but in my Fortran code I am able to run only one file at a time. How should I call the Fortran program in python?

我有一个 Fortran 程序,想在 python 中为多个文件执行它。我有 2000 个输入文件,但在我的 Fortran 代码中,我一次只能运行一个文件。我应该如何在python中调用Fortran程序?

My Script:

我的脚本:

import subprocess
import glob
input = glob.glob('C:/Users/Vishnu/Desktop/Fortran_Program_Rum/*.txt')
output = glob.glob('C:/Users/Vishnu/Desktop/Fortran_Program_Rum/Output/')
f = open("output", "w")
for i in input:
    subprocess.Popen(["FORTRAN ~/C:/Users/Vishnu/Desktop/Fortran_Program_Rum/phase1.f", "--domain "+i])
f.write(i)

Error:

错误:

runfile('C:/Users/Vishnu/Desktop/test_fn/test.py', wdir='C:/Users/Vishnu/Desktop/test_fn')
Traceback (most recent call last):

   File "<ipython-input-3-f8f378816004>", line 1, in <module>
runfile('C:/Users/Vishnu/Desktop/test_fn/test.py', wdir='C:/Users/Vishnu/Desktop/test_fn')

  File "C:\Users\Vishnu\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 866, in runfile
execfile(filename, namespace)

  File "C:\Users\Vishnu\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)

  File "C:/Users/Vishnu/Desktop/test_fn/test.py", line 30, in <module>
subprocess.Popen(["FORTRAN ~/C:/Users/Vishnu/Desktop/Fortran_Program_Rum/phase1.f", "--domain "+i])

  File "C:\Users\Vishnu\Anaconda3\lib\subprocess.py", line 707, in __init__
restore_signals, start_new_session)

  File "C:\Users\Vishnu\Anaconda3\lib\subprocess.py", line 990, in _execute_child
startupinfo)

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

Edit:

编辑:

import subprocess
import os
input = os.path.normcase(r'C:/Users/Vishnu/Desktop/Fortran_Program_Rum/*.txt')
output = os.path.normcase(r'~/C:/Users/Vishnu/Desktop/Fortran_Program_Rum/Output/')
f = open("output", "w")
for i in input:
    exe = os.path.normcase(r'~/C:/Program Files (x86)/Silverfrost/ftn95.exe')
    fortran_script = os.path.normcase(r'~/C:/Users/Vishnu/Desktop/test_fn/test_f2py.f95')
    i = os.path.normcase(i)
    subprocess.Popen([exe, fortran_script, "--domain", i])
    f.write(i)

Error:

错误:

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

Edit - 2:

编辑 - 2:

I have change my script as below: but error is same

我已经改变了我的脚本如下:但错误是一样的

input = os.path.normcase(r'C:/Users/Vishnu/Desktop/Fortran_Program_Rum/*.txt')
output = os.path.normcase(r'C:/Users/Vishnu/Desktop/Fortran_Program_Rum/Output/')
f = open("output", "w")
for i in input:
    exe = os.path.normcase(r'C:/Program Files (x86)/Silverfrost/ftn95.exe')
    fortran_script = os.path.normcase(r'C:/Users/Vishnu/Desktop/test_fn/test_f2py.f95')
    i = os.path.normcase(i)
    subprocess.Popen([exe, fortran_script, "--domain", i])
    f.write(i)

Error: 2

错误:2

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

Error: 3 - 15-03-2017

错误:3 - 15-03-2017

import subprocess
import os
input = os.path.normcase(r'C:/Users/Vishnu/Desktop/Fortran_Program_Rum/*.txt')
output = os.path.normcase(r'C:/Users/Vishnu/Desktop/Fortran_Program_Rum/Output/')
f = open('output', 'w+')
for i in input:
    exe = os.path.normcase(r'C:/Program Files (x86)/Silverfrost/ftn95.exe')
    fortran_script = os.path.normcase(r'C:/Users/Vishnu/Desktop/Fortran_Program_Rum/phase1.f')
    i = os.path.normcase(i)
    subprocess.Popen([exe, fortran_script, "--domain", i], shell = True)
    f.write(i)

** Error **

** 错误 **

PermissionError: [Errno 13] Permission denied: 'output'

回答by nevihs

Popen expect a list of strings for non-shell calls and a string for shell calls.

Popen 需要一个用于非 shell 调用的字符串列表和一个用于 shell 调用的字符串。

Call subprocess.Popen with shell=True:

使用 shell=True 调用 subprocess.Popen:

process = subprocess.Popen(command, stdout=tempFile, shell=True)

Hopefully this solves your issue.

希望这可以解决您的问题。

This issue is listed here: https://bugs.python.org/issue17023

这里列出了这个问题:https: //bugs.python.org/issue17023

回答by Edwin van Mierlo

I believe you need to .ffile as a parameter, not as a command-single-string. same with the "--domain "+i, which i would split in two elements of the list. Assuming that:

我相信您需要将.f文件作为参数,而不是作为命令单字符串。与 相同"--domain "+i,我会将其拆分为列表的两个元素。假如说:

  • you have the path set for FORTRANexecutable,
  • the ~/is indeed the correct way for the FORTRANexecutable
  • 您已为FORTRAN可执行文件设置了路径,
  • ~/确实是FORTRAN可执行文件的正确方法

I would change this line:

我会改变这一行:

subprocess.Popen(["FORTRAN ~/C:/Users/Vishnu/Desktop/Fortran_Program_Rum/phase1.f", "--domain "+i])

to

subprocess.Popen(["FORTRAN", "~/C:/Users/Vishnu/Desktop/Fortran_Program_Rum/phase1.f", "--domain", i])

If that doesn't work, you should do a os.path.exists()for the .ffile, and check that you can launch the FORTRANexecutable without any path, and set the path or system path variable accordingly

如果这不起作用,您应该os.path.exists().f文件做一个,并检查您是否可以在FORTRAN没有任何路径的情况下启动可执行文件,并相应地设置路径或系统路径变量

[EDIT 6-Mar-2017]

[编辑 2017 年 3 月 6 日]

As the exception, detailed in the original post, is a python exception from subprocess; it is likely that the WinError 2is because it cannot find FORTRAN

作为例外,在原帖中有详细说明,是来自subprocess;的 python 例外。很可能WinError 2是因为它找不到FORTRAN

I highly suggest that you specify full path for your executable:

我强烈建议您为可执行文件指定完整路径:

for i in input:
    exe = r'c:\somedir\fortrandir\fortran.exe'
    fortran_script = r'~/C:/Users/Vishnu/Desktop/Fortran_Program_Rum/phase1.f'
    subprocess.Popen([exe, fortran_script, "--domain", i])

if you need to convert the forward-slashes to backward-slashes, as suggested in one of the comments, you can do this:

如果您需要按照评论之一中的建议将正斜杠转换为反斜杠,您可以这样做:

for i in input:
    exe = os.path.normcase(r'c:\somedir\fortrandir\fortran.exe')
    fortran_script = os.path.normcase(r'~/C:/Users/Vishnu/Desktop/Fortran_Program_Rum/phase1.f')
    i = os.path.normcase(i)
    subprocess.Popen([exe, fortran_script, "--domain", i])

[EDIT 7-Mar-2017]

[编辑 7-Mar-2017]

The following line is incorrect:

以下行是不正确的:

exe = os.path.normcase(r'~/C:/Program Files (x86)/Silverfrost/ftn95.exe'

I am not sure why you have ~/as a prefix for every path, don't do that.

我不知道为什么~/每个路径都有前缀,不要这样做。

for i in input:
    exe = os.path.normcase(r'C:/Program Files (x86)/Silverfrost/ftn95.exe'
    fortran_script = os.path.normcase(r'C:/Users/Vishnu/Desktop/Fortran_Program_Rum/phase1.f')
    i = os.path.normcase(i)
    subprocess.Popen([exe, fortran_script, "--domain", i])

[2nd EDIT 7-Mar-2017]

[2017 年 3 月 7 日第二次编辑]

I do not know this FORTRAN or ftn95.exe, does it need a shell to function properly?, in which case you need to launch as follows:

我不知道这个 FORTRAN 或 ftn95.exe,它是否需要 shell 才能正常运行?,在这种情况下,您需要按如下方式启动:

subprocess.Popen([exe, fortran_script, "--domain", i], shell = True)

You really need to try to launch the command manually from the working directory which your python script is operating from. Once you have the command which is actually working, then build up the subprocesscommand.

您确实需要尝试从运行 python 脚本的工作目录手动启动命令。一旦你有了实际工作的subprocess命令,然后建立命令。

回答by Sphynx-HenryAY

thank you, your first error guides me here and the solution solve mine too!

谢谢,您的第一个错误引导我到这里,解决方案也解决了我的问题!

for permission error, f = open('output', 'w+'), change it into f = open(output+'output', 'w+').

对于权限错误,f = open('output', 'w+')将其更改为f = open(output+'output', 'w+').

or something else, but the way you are now using is having access to the installation directory of Python which normally in Program Files, and it probably needs administrator permission.

或其他东西,但您现在使用的方式是可以访问通常在 Program Files 中的 Python 安装目录,并且它可能需要管理员权限。

for sure, you could probably running python/your script as administrator to pass permission error though

当然,您可能可以以管理员身份运行 python/您的脚本来传递权限错误