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

38

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

我的脚本:

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)

错误:

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

编辑:

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)

错误:

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

编辑 - 2:

我已经按照下面的方式更改了我的脚本:但是错误仍然存在。

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)

错误:2

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

错误:3 - 2017年3月15日

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)

** 错误 **

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

我不了解Fortran,但它是否像Python一样支持Windows路径中的正斜杠/?您可能需要在命令行中将正斜杠转换为反斜杠。 - Guillaume
3
我建议您使用subprocess.run()代替已经过时的subprocess.Popen()。请参考https://docs.python.org/3/library/subprocess.html#subprocess.run。 - Guillaume
5个回答

59

Popen 期望在非 shell 调用时传入字符串列表,而在 shell 调用时传入字符串。

使用 shell=True 调用 subprocess.Popen:

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

希望这解决了您的问题。

此问题已列在此处: https://bugs.python.org/issue17023


3

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

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

或者其他方法,但现在您正在使用的方式可以访问Python安装目录,通常位于Program Files,并且可能需要管理员权限。

当然,您可以将python/脚本作为管理员运行以通过权限错误。


2
我相信你需要将.f文件作为参数,而不是作为一个命令单字符串。同样的,对于"--domain "+i,我会将其拆分为列表的两个元素。
假设:
  • 你已经设置了FORTRAN可执行文件的路径,
  • ~/确实是正确的FORTRAN可执行文件的路径。
我会更改这一行:
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])

如果那不起作用,应该对 .f 文件执行 os.path.exists() 操作,并检查是否可以启动 FORTRAN 可执行文件而不需要任何路径,并相应地设置路径或系统路径变量。
[编辑于2017年3月6日] 由于异常在原帖中是来自 subprocess 的 Python 异常,因此很可能 WinError 2 是因为找不到 FORTRAN。 我强烈建议您为可执行文件指定完整路径。
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])

如果您需要将正斜杠转换为反斜杠,如评论中建议的那样,您可以执行以下操作:

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])

[编辑 2017年3月7日]
以下一行文字不正确:
exe = os.path.normcase(r'~/C:/Program Files (x86)/Silverfrost/ftn95.exe'

我不确定为什么你在每个路径前面都加上了~/,不要这样做。

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])

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

我不了解这个FORTRAN或ftn95.exe,它是否需要一个shell才能正常运行?如果是这样,您需要按照以下方式启动:

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

您真的需要尝试从Python脚本所在的工作目录手动启动命令。一旦您有了实际工作的命令,然后再构建subprocess命令。


2
我已经尝试了两种方法,但没有得到结果。您能否请用代码示例来解释一下? - Jone
谢谢,但我仍然遇到相同的错误。我已经编辑了我的代码和上面的错误。 - Jone
我已经按照您的建议更新了我的脚本,但仍然遇到相同的错误。我已经编辑了我的问题“编辑-2”。 - Jone
仍然没有得到输出。请检查上面编辑的问题并建议我。 - Jone
你的变量 output 指向一个目录,而不是一个文件。然后你尝试用 'output' 这个名称打开一个文件,但这肯定是当前工作目录中的文件,而不是在 output 变量所指向的目录中的文件。你需要更精确地说明你想要写入文件的内容和位置。 - Edwin van Mierlo

0
我遇到了相同的错误,当我在路径中使用“\\”代替“/”时,问题得到了解决。
而不是

0

我对类似问题的解决方案是使用os包。

_command = f"ffmpeg -i \"{src_path}\" -ss 00:00:00.000 -vframes 1 \"{dist_path}\""
os.system(_command)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接