0

I have a python code that goes as

for x in name:
     print(x)
     os.system('exe file')#prints output1
     os.system('2nd exe file)#prints output2

it is printing

output1
x
output2

why is it happening?

Mike Müller
  • 82,630
  • 20
  • 166
  • 161

1 Answers1

0

This has been aswered here. Check answers for ways to solve it.

When you're outputting to a pipe, Python buffers your output that's written to sys.stdout and outputs it after a flush, or after it's overflown, or upon closing (when the program exits). While it'll buffer the print calls, system calls output directly into stdout and their output won't be buffered. That's why you're seeing such precedence. To avoid that, use python -u:

python -u test.py > test.out; cat test.out

See more info here.

1: Disable output buffering

Felipe Trenk
  • 341
  • 2
  • 8
  • At your reputation level, you can't yet flag as duplicate. Until you can, please simply abstain from answering duplicates. Thanks. – tripleee Dec 28 '17 at 11:46