我有一个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'
答案 0 :(得分:7)
Popen期望非shell调用的字符串列表和shell调用的字符串。
使用shell = True调用subprocess.Popen:
process = subprocess.Popen(command, stdout=tempFile, shell=True)
希望这可以解决您的问题。
此问题列在此处: https://bugs.python.org/issue17023
答案 1 :(得分:1)
我认为你需要.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])
如果这不起作用,您应该为os.path.exists()
文件执行.f
,并检查您是否可以在没有任何路径的情况下启动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 :(得分:1)
谢谢你,你的第一个错误指导我,解决方案也解决了我的问题!
对于权限错误f = open('output', 'w+')
,请将其更改为f = open(output+'output', 'w+')
。
或其他,但你现在使用的方式是访问通常在Program Files中的Python安装目录,它可能需要管理员权限。
当然,您可能以管理员身份运行python /您的脚本以通过权限错误