os.system is a python standard library function. It executes the given string argument as a command in a subshell.
Questions tagged [os.system]
638 questions
137
votes
6 answers
Python: How to get stdout after running os.system?
I want to get the stdout in a variable after running the os.system call.
Lets take this line as an example:
batcmd="dir"
result = os.system(batcmd)
result will contain the error code (stderr 0 under Windows or 1 under some linux for the above…

Eduard Florinescu
- 16,747
- 28
- 113
- 179
58
votes
10 answers
Linux command-line call not returning what it should from os.system?
I need to make some command line calls to linux and get the return from this, however doing it as below is just returning 0 when it should return a time value, like 00:08:19, I am testing the exact same call in regular command line and it returns…

Rick
- 16,612
- 34
- 110
- 163
48
votes
6 answers
Python try block does not catch os.system exceptions
I have this python code:
import os
try:
os.system('wrongcommand')
except:
print("command does not work")
The code prints:
wrongcommand: command not found
Instead of command does not work. Does anyone know why it's not printing my error…

Cinder
- 1,599
- 2
- 13
- 23
39
votes
6 answers
Python os.system without the output
I'm running this:
os.system("/etc/init.d/apache2 restart")
It restarts the webserver, as it should, and like it would if I had run the command directly from the terminal, it outputs this:
* Restarting web server apache2 …

user697108
- 3,521
- 3
- 20
- 14
33
votes
2 answers
Return value of x = os.system(..)
When I type os.system("whoami") in Python, as root, it returns root, but when I try to assign it to a variable x = os.system("whoami") it set's the value of x to 0. Why ? (:

Ramon
- 341
- 1
- 3
- 4
31
votes
2 answers
Advantages of subprocess over os.system
I have recently came across a few posts on stack overflow saying that subprocess is much better than os.system, however I am having difficulty finding the exact advantages.
Some examples of things I have run…

Lain
- 2,166
- 4
- 23
- 47
29
votes
2 answers
What is a practical difference between check_call check_output call, and Popen methods in the subprocess module?
Honestly, I just don't understand the lingo of "non-zero" status to really interpret what's going on or what that means (it wasn't even defined) on help pages. What are some examples of using python to call other scripts in which these processes of…

Tom
- 919
- 3
- 9
- 22
21
votes
1 answer
How to get the output from os.system()?
I want to get output from os.system("nslookup google.com") but instead I always get 0, when printing it. Why is this, and how can I fix this? (Python 3, Mac)
(I looked at How to store the return value of os.system that it has printed to stdout in…

KamilDev
- 718
- 2
- 7
- 21
18
votes
3 answers
Why is python no longer waiting for os.system to finish?
I have the following function, which has been working great for months. I have not updated my version of Python (unless it happens behind the scenes?).
def Blast(type, protein_sequence, start, end, genomic_sequence):
result = []
M =…

Stylize
- 1,058
- 5
- 16
- 32
13
votes
4 answers
Python 'source HOME/.bashrc' with os.system()
I am writing a python script (Linux) that is adding some shell aliases (writes them to HOME/.bash_aliases).
In order to make an alias available immediately after it was written I should issue the following bash built-in:
source HOME/.bashrc
source…

Andrei Ciobanu
- 12,500
- 24
- 85
- 118
13
votes
2 answers
return value from one python script to another
I have two files: script1.py and script2.py. I need to invoke script2.py from script1.py and return the value from script2.py back to script1.py. But the catch is script1.py actually runs script2.py through os.
script1.py:
import…

codingsplash
- 4,785
- 12
- 51
- 90
12
votes
2 answers
passing more than one variables to os.system in python
I want to pass two variables to the os.system() for example listing files in different format in specific directory like (ls -l testdirectory) in which both a switch and test directory are variable.
I know for single variable this one…

hamed
- 1,325
- 1
- 15
- 18
11
votes
5 answers
Redirecting stdio from a command in os.system() in Python
Usually I can change stdout in Python by changing the value of sys.stdout. However, this only seems to affect print statements. So, is there any way I can suppress the output (to the console), of a program that is run via the os.system() command…

Leif Andersen
- 21,580
- 20
- 67
- 100
11
votes
2 answers
Send SIGINT in python to os.system
I am trying to run a Linux command strace -c ./client in python with os.system(). When I press Ctrl + C I get some output on the terminal. I have to send the "Process correctly halted" signal programmatically after one minute and want the terminal…

user2591307
- 119
- 1
- 2
- 6
11
votes
3 answers
How to determine pid of process started via os.system
I want to start several subprocesses with a programm, i.e. a module foo.py starts several instances of bar.py.
Since I sometimes have to terminate the process manually, I need the process id to perform a kill command.
Even though the whole setup is…

Sebastian Werk
- 1,568
- 2
- 17
- 30