0

i have the following loop with a print and a subprocess call

for file in os.listdir(dir):
    print(file)
    subprocess.call(['python', 'otherscript.py', file])

otherscript.py prints some stuff as well. so when i execute my main script, everything that my main script should print before calling otherscript.py will be printed after otherscript.py is called for the last time:

  • output from subprocess 1
  • output from subprocess 2
  • output from subprocess 3
  • output from main 1
  • output from main 2
  • output from main 3

how can i make it print before calling the subprocess?

johnson
  • 3,729
  • 3
  • 31
  • 32

1 Answers1

1

The subprocess stdout buffer is flushed then the child python script exits while the content buffered by print() function in the parent may still be there.

The solution is to make sure that nothing is buffered before running subprocess.call(). See How to flush output of Python print?

Community
  • 1
  • 1
jfs
  • 399,953
  • 195
  • 994
  • 1,670