I was trying to write an automated task with Python3 and using subprocess.call
as I need to run some shell commands as part of the script, but found one of the outputs was not printing in the right order as I expected.
Portion of it:
On Python2:
[root@server101 ~]# cat check_kernel.py
#!/usr/bin/env python
import subprocess
print "Current running kernel version on the system is:\n", subprocess.call(["uname", "-r"])
Output:
[root@server101 ~]# ./check_kernel.py
Current running kernel version on the system is:
3.10.0-1160.45.1.el7.x86_64
0
On Python3:
[root@server101 ~]# cat check_kernel.py
#!/usr/bin/env python3
import subprocess
print ("Current running kernel version on the system is:\n", subprocess.call(["uname", "-r"]))
Output:
[root@server101 ~]# ./check_kernel.py
3.10.0-1160.45.1.el7.x86_64
Current running kernel version on the system is:
0
So what is the real difference there happening with subprocess.call
when working with Python3?.
Am I missing something or I shouldn't be using subprocess.call
with Python3 and changing it to subprocess.run
or subprocess.Popen
is the only way to get subprocess worked with Python3 ?.