2

I have a bash script that executes a python script like.

python script_name.py >> output.log 2>&1

As you can see I want standard output and std error in the same file called output.log.

However, the script_name.py is executing a system command using os.system method. Let's assume following is the python script.

import os
print('Executing python script')
os.system('echo Helloworld')
print('python script finished')

Now the output.log file is getting the output from the python script. But the output from os.system method is being logged first. So the output.log file is having the output as:

Helloworld
Executing python script
python script finished

But I want the print statement to be logged first.

However, If I execute the script directly without the bash script. I get the following output in the console.

Executing python script
Helloworld
python script finished
Amarjit Singh
  • 2,068
  • 19
  • 52
  • 2
    If you give `flush = true` as an argument to both of those `print()` calls, does it fix the problem? If so, this is a quirk of buffered standard output. – Green Cloak Guy Apr 20 '20 at 17:10

0 Answers0