pythonsubprocesswinmerge

How to start a process only after the previous one is closed


I am trying to compare a list of files existing in a folder with some in another folder using Winmerge. I would like the first comparison to be opened in Winmerge and upon its closure, the second comparison is opened so on and so forth until there are no more files to be compared.

I have tried calling subprocess.Popen() in a loop for all files, but this launches multiple Winmerge windows.

for file in file_list:
    get_comparison = subprocess.Popen('WinmergeU.exe ' +'D:\database_1\'+file +'D:\database_2\'+file, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)

I expect only one process of Winmerge to be running at a time


Solution

  • You can use Popen.wait() method to wait for a process to end. Or just use subprocess.run or subprocess.getstatusoutput or getoutput, etc.

    import subprocess
    
    if __name__ == '__main__':
        p = subprocess.Popen('echo 1', stdout=subprocess.PIPE)
        code = p.wait()
        print('first task finished', code)
    
        p = subprocess.run('echo 2', stdout=subprocess.PIPE)
        print('second task finished', p.returncode, p.stdout.decode())
    
        code, output = subprocess.getstatusoutput('echo 3')
        print('third task finished', code, output)
    
    

    output:

    first task finished 0
    second task finished 0 2
    third task finished 0 3