【问题标题】:WinError 2 The system cannot find the file specified (Python)WinError 2 系统找不到指定的文件(Python)
【发布时间】:2017-03-03 06:58:37
【问题描述】:

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

【问题讨论】:

  • 我不知道 Fortran,但它是否支持 Python 等 Windows 路径中的正斜杠 / ?您可能需要在命令行中将正斜杠转换为反斜杠。
  • 我也鼓励你使用subprocess.run()而不是旧的subprocess.Popen()docs.python.org/3/library/subprocess.html#subprocess.run

标签: python python-2.7 python-3.x bioinformatics f2py


【解决方案1】:

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

使用 shell=True 调用 subprocess.Popen:

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

希望这能解决您的问题。

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

【讨论】:

    【解决方案2】:

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

    权限错误,f = open('output', 'w+'),改成f = open(output+'output', 'w+')。

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

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

    【讨论】:

      【解决方案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可执行文件,并相应地设置路径或系统路径变量

      [编辑 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])
      

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

      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 命令。

      【讨论】:

      • 我都试过了,但没有得到结果..你能用代码示例解释一下吗。
      • 谢谢,但我仍然遇到同样的错误。我已经编辑了上面的代码和错误。
      • 我已按照您的建议更新了我的脚本,但仍然出现同样的错误。我已经编辑了我的问题Edit -2
      • 仍然没有得到输出。请检查上面的编辑问题并建议我
      • 你的变量output,指向一个目录,而不是一个文件,然后你尝试open一个名为'output'的文件,但这肯定是当前工作中的一个文件目录,而不是您在 output 变量中的目录中的文件。您需要更精确地确定要写入文件的内容和位置。
      猜你喜欢
      • 2020-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-28
      相关资源
      最近更新 更多