8

I have simple Python script to execute test suite both under Windows and Linux. Every test writes its output to separate file. I use subprocess.Popen class to execute shell command in a cycle.

Every shell command starts like that:

def system_execute(self, command, path, out_file):
    params_list = command.split(' ') 
    file_path = os.path.join(path, out_file)
    f = open(file_path, "w")
    subprocess.Popen(params_list, stdout=f)
    f.close()

It works fine, but script finishes its work before all output files have been written. Actually, I getting hundreds of zero-size files, and it takes some time to finish writing output and close handles. Could anyone explain the reason why it works so strange, and is there a synchronous way to do the same job?

Thanks

1 Answer 1

16

Before f.close(), you have to wait() for our subprocess.

def system_execute(self, command, path, out_file):
    params_list = command.split(' ') 
    file_path = os.path.join(path, out_file)
    f = open(file_path, "w")
    sp = subprocess.Popen(params_list, stdout=f)
    sp.wait()
    f.close()

or just

def system_execute(self, command, path, out_file):
    params_list = command.split(' ') 
    file_path = os.path.join(path, out_file)
    f = open(file_path, "w")
    subprocess.call(params_list, stdout=f)
    f.close()

(or, for easier file handling,

[...]
    with open(file_path, "w") as f:
        subprocess.call(params_list, stdout=f)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.