Search code examples
pythonpython-2.7io

python on linux can't find file inside function


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?


Solution

  • This is probably a race condition: When your code wants to read the output from the file, the program isn't finished yet and the output file doesn't exist yet. Try a loop with "sleep" waiting for the file you want to read to become available. Another possibility is that that the file permissions don't match.