I have a Fortran program and want to execute it in python for multiple files. I have 2000 input files but in my Fortran code I am able to run only one file at a time. How should I call the Fortran program in python?
My Script:
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)
Error:
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
Edit:
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)
Error:
FileNotFoundError: [WinError 2] The system cannot find the file specified
Edit - 2:
I have change my script as below: but error is same
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)
Error: 2
FileNotFoundError: [WinError 2] The system cannot find the file specified
Error: 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)
** Error **
PermissionError: [Errno 13] Permission denied: 'output'
Run CHKDSK Command to Fix "System Cannot Find File Specified" Device. Right-click the Start button, type cmd in the Search, and select Command Prompt (Admin). Type chkdsk x: /f /r (x represents your target drive) into the Command Prompt window and press Enter Wait while chkdsk tries to repair the corrupted file systems ...
What Is the FileNotFoundError: [WinError 2] The system cannot find the file specified in Python. The FileNotFoundError is an error that occurs when a file cannot be found. This can be due to many reasons, such as the file being deleted, moved, or renamed. It can also occur if the file never existed in the first place.
Popen expect a list of strings for non-shell calls and a string for shell calls.
Call subprocess.Popen with shell=True:
process = subprocess.Popen(command, stdout=tempFile, shell=True)
Hopefully this solves your issue.
This issue is listed here: https://bugs.python.org/issue17023
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With