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

2024-05-13 08:14:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个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-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)

**错误**

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

Tags: pathintestinputoutputosprogramexe
3条回答

我认为需要将.f文件作为参数,而不是作为命令单个字符串。与"--domain "+i相同,我将把它分成列表的两个元素。 假设:

  • 您已经为FORTRAN可执行文件设置了路径
  • ~/确实是FORTRAN可执行文件的正确方式

我要改这行:

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

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

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

[编辑日期: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命令。

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

使用shell=True调用subprocess.Popen:

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

希望这能解决你的问题。

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

谢谢你,你的第一个错误指引我来到这里,解决我的问题!

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

或者其他一些东西,但是您现在使用的方法是访问Python的安装目录,通常在程序文件中,它可能需要管理员权限。

当然,您可以作为管理员运行python/脚本来传递权限错误

相关问题 更多 >