Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WinError 2 The system cannot find the file specified (Python)

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' 
like image 465
Jone Avatar asked Mar 03 '17 06:03

Jone


People also ask

How do I fix the system Cannot find the file specified?

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 winerror2?

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.


1 Answers

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

like image 149
nevihs Avatar answered Sep 20 '22 13:09

nevihs