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

Jon*_*one 9 python bioinformatics f2py python-2.7 python-3.x

我有一个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)
Run Code Online (Sandbox Code Playgroud)

错误:

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
Run Code Online (Sandbox Code Playgroud)

编辑:

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)
Run Code Online (Sandbox Code Playgroud)

错误:

FileNotFoundError: [WinError 2] The system cannot find the file specified
Run Code Online (Sandbox Code Playgroud)

编辑 - 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)
Run Code Online (Sandbox Code Playgroud)

错误:2

FileNotFoundError: [WinError 2] The system cannot find the file specified
Run Code Online (Sandbox Code Playgroud)

错误: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)
Run Code Online (Sandbox Code Playgroud)

**错误**

PermissionError: [Errno 13] Permission denied: 'output'
Run Code Online (Sandbox Code Playgroud)

nev*_*ihs 14

Popen期望非shell调用的字符串列表和shell调用的字符串.

使用shell = True调用subprocess.Popen:

process = subprocess.Popen(command, stdout=tempFile, shell=True)
Run Code Online (Sandbox Code Playgroud)

希望这能解决您的问题.

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


Edw*_*rlo 2

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

  • 您已设置可执行文件的路径FORTRAN
  • 这确实是可执行文件~/的正确方法FORTRAN

我会改变这一行:

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

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

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

[2017 年 3 月 6 日编辑]

作为异常,在原始帖子中详细说明,是来自 ; 的 python 异常subprocess。很可能是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])
Run Code Online (Sandbox Code Playgroud)

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

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])
Run Code Online (Sandbox Code Playgroud)

[编辑 2017 年 3 月 7 日]

以下行是不正确的:

exe = os.path.normcase(r'~/C:/Program Files (x86)/Silverfrost/ftn95.exe'
Run Code Online (Sandbox Code Playgroud)

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

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])
Run Code Online (Sandbox Code Playgroud)

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

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

subprocess.Popen([exe, fortran_script, "--domain", i], shell = True)
Run Code Online (Sandbox Code Playgroud)

您确实需要尝试从 python 脚本运行的工作目录手动启动命令。一旦您拥有实际运行的命令,就可以构建该subprocess命令。

  • 我都尝试过,但没有得到结果..你能用代码示例解释一下吗? (2认同)