0

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

ijaySha
  • 61
  • 8

2 Answers2

0

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
abdusco
  • 9,700
  • 2
  • 27
  • 44
0

subprocess.Popen() doesn't block, it just creates processes i.e. the program does not wait for each process to complete before spawning a new one. I'm not sure what version of Python you're working in, but:

  • If you're working in Python 3.7.X, use subprocess.run() instead.
  • If you're working in Python 2.7.X, use subprocess.call() instead.

These methods do block and wait for each process to complete before starting the next one. It's not obvious at first, but you should find it in the subprocess documentation.

dkearn
  • 441
  • 4
  • 5