I'm writing a script on python 2.7.12 on linux with a bunch of functions that run external programs...
at some point, I have a function that runs a program, which generates an external file. The next step of the function is to read the external file and do some processing.
The function is
def RunProgram(input, options)
input_file=str(input)
options=str(options)
cmd=str('program + ' input_file + '--flag ' + options + ' --out temp.log &> /dev/null')
#print(cmd)
#print(cmd)
os.system(cmd)
with open('path_to_file/temp.log') as fp:
for i, line in enumerate(fp):
if i == 2:
#print(line)
solution=str(line) #stores 3rd line of log file
elif i > 2:
break
return solution
Somehow, although the external program runs and I see temp.log created from a bash shell, the function exits with the error
IOError: [Errno 2] No such file or directory: 'path_to_file/temp.log'
if in the function after os.system(cmd) I place
print(os.path.exists('path_to_file/temp.log'))
print(os.path.isfile('path_to_file/temp.log'))
I get 2 false, but if I run those commands after running the function and getting the error i get True for both and I see the file in the directory using ls.
Once I run the function again with temp.log already existing, the function works.
I have checked with os.getcwd() and the session is running in the correct dirrectory. I also checked I have read permissions on temp.log
..any ideas?