我有一个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'
我认为需要将
.f
文件作为参数,而不是作为命令单个字符串。与"--domain "+i
相同,我将把它分成列表的两个元素。 假设:FORTRAN
可执行文件设置了路径~/
确实是FORTRAN
可执行文件的正确方式我要改这行:
到
如果这不起作用,您应该为
.f
文件执行os.path.exists()
,并检查是否可以在不使用任何路径的情况下启动FORTRAN
可执行文件,并相应地设置path或系统path变量[编辑日期:2017年3月6日]
因为在原始文章中详细描述的异常是来自
subprocess
的python异常;WinError 2
很可能是因为它找不到FORTRAN
我强烈建议您指定可执行文件的完整路径:
如果需要将正斜杠转换为反斜杠(如其中一条注释中所建议的),可以执行以下操作:
[编辑日期:2017年3月7日]
以下行不正确:
我不知道为什么每个路径都以
~/
作为前缀,不要这样做。【第二次编辑:2017年3月7日】
我不知道这个FORTRAN或ftn95.exe,它需要一个shell才能正常工作吗?,在这种情况下,需要按如下方式启动:
您确实需要尝试从python脚本正在运行的工作目录中手动启动该命令。一旦得到了实际工作的命令,就构建
subprocess
命令。Popen需要非shell调用的字符串列表和shell调用的字符串列表。
使用shell=True调用subprocess.Popen:
希望这能解决你的问题。
此问题列在此处: https://bugs.python.org/issue17023
谢谢你,你的第一个错误指引我来到这里,解决我的问题!
对于权限错误,
f = open('output', 'w+')
,将其更改为f = open(output+'output', 'w+')
。或者其他一些东西,但是您现在使用的方法是访问Python的安装目录,通常在程序文件中,它可能需要管理员权限。
当然,您可以作为管理员运行python/脚本来传递权限错误
相关问题 更多 >
编程相关推荐