If all you need is the stdout output, then take a look at subprocess.check_output():

import subprocess

batcmd="dir"
result = subprocess.check_output(batcmd, shell=True)

Because you were using os.system(), you'd have to set shell=True to get the same behaviour. You do want to heed the security concerns about passing untrusted arguments to your shell.

If you need to capture stderr as well, simply add stderr=subprocess.STDOUT to the call:

result = subprocess.check_output([batcmd], stderr=subprocess.STDOUT)

to redirect the error output to the default output stream.

If you know that the output is text, add text=True to decode the returned bytes value with the platform default encoding; use encoding="..." instead if that codec is not correct for the data you receive.

Answer from Martijn Pieters on Stack Overflow
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ print-output-from-os-system-in-python
Print Output from Os.System in Python - GeeksforGeeks
July 23, 2025 - ... The os.system() function takes ... Unix-like system to list the files in the current directory: ... This command will print the output directly to the console....
Discussions

geoserver rest api - How we can get output for os.system() in python? - Geographic Information Systems Stack Exchange
I wrote the following command in Geoserver rest-api using curl: cmd=curl -v -u admin:geoserver -XGET -H "Accept: text/xml" http://localhost:8080/geoserver/rest/workspaces/myworkspace/datastores/ More on gis.stackexchange.com
๐ŸŒ gis.stackexchange.com
August 27, 2014
python - Assign output of os.system to a variable and prevent it from being displayed on the screen - Stack Overflow
I want to assign the output of a command I run using os.system to a variable and prevent it from being output to the screen. But, in the below code ,the output is sent to the screen and the value p... More on stackoverflow.com
๐ŸŒ stackoverflow.com
how to get the stdout of the script that os.system executes - Post.Byes
If i use "if os.system(cmd) != 0:" to check it before running "var=commands.g etoutput(cmd)", then the stdout will not only be written to variable var, but also pop on the prompt screen, which is not what I like.. Does anyone have some idea? commands.getsta tusoutput [code=python] >>> fail = commands.getsta tusoutput("ps -q") >>> notfail = commands.getsta tusoutput("ps -A") >>> fail[0] 256 >>> len(fail[1]) # the output ... More on post.bytes.com
๐ŸŒ post.bytes.com
October 31, 2007
python + how to print value that comes from os.system - Unix & Linux Stack Exchange
0 python os.system: syntax error near unexpected token `(' - nested parentheses More on unix.stackexchange.com
๐ŸŒ unix.stackexchange.com
January 21, 2018
๐ŸŒ
Medium
pramodgupta12378.medium.com โ€บ mastering-python-storing-command-output-in-variables-419e030c0f43
Mastering Python: Storing Command Output in Variables | by Pramod Kumar Gupta | Medium
July 17, 2023 - import platform import subprocess def get_system_info(): # Retrieve system information using 'platform' module system_info = { 'System': platform.system(), 'Node Name': platform.node(), 'Release': platform.release(), 'Version': platform.version(), 'Machine': platform.machine(), 'Processor': platform.processor() } return system_info def get_disk_space(): # Get disk space information using 'df' command and store it in a variable disk_space_info = subprocess.run(['df', '-h'], capture_output=True, text=True) return disk_space_info.stdout if __name__ == "__main__": system_info = get_system_info() p
๐ŸŒ
DigitalOcean
digitalocean.com โ€บ community โ€บ tutorials โ€บ python-system-command-os-subprocess-call
Python os.system() vs subprocess: Run System Commands | DigitalOcean
1 month ago - The rest of this tutorial covers each of these methods in detail with working code examples. The os.system() function executes a command string through the system shell and returns the exit status of the command. It is the most basic way to run an external command from Python...
Find elsewhere
๐ŸŒ
Post.Byes
post.bytes.com โ€บ home โ€บ forum โ€บ topic โ€บ python
how to get the stdout of the script that os.system executes - Post.Byes
October 31, 2007 - Then all I have to do is read the file. [CODE=python]>>> os.system("echo hello > output.txt") 0 >>> reader = open("output.tx t") >>> print reader.read() hello [/CODE] If you want to just append to the file instead of truncating then appending, use ">>" instead of ">".
๐ŸŒ
Python Forum
python-forum.io โ€บ thread-2239.html
os.system Output
So a new one for me. Any help appreciated. I would no doubt be able to find the answer eventually, but its nice to talk to other humans from time to time. I am trying to query AD at a point in my code, to check to see if a host exists on the doma...
๐ŸŒ
LearnByExample
learnbyexample.github.io โ€บ 100_page_python_intro โ€บ executing-external-commands.html
Executing external commands - 100 Page Python Intro
>>> import os >>> os.system('echo hello "$USER"') hello learnbyexample 0 ยท Similar to the print() function, the output of the external command, if any, is displayed on the screen.
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ assigning variable from os.system()
r/learnpython on Reddit: assigning variable from os.system()
September 6, 2014 -

I am having trouble assigning the output of os.system() as a variable.

When I do the following, it seems to produce the correct output:

>>> var = os.system('date +"%s"')
1409938484

However, when I try to print my variable, I get the status code instead:

>>> print var
0

Just entering os.system() into the interpreter gives me the following:

>>> os.system('date +"%s"')
1409938815
0

How do I set the variable to the output, not the status code?

Do I really need to use subprocess to call the command and then read stdout to do this? (this seems overly complex for what i am trying to do here)

edit

Here is what I came up with after some searching:

>>> var = os.popen('date +"%s"').readline().strip('\n')
>>> print var
1409940330
>>> 

It's kinda verbose (and I thought os.popen was deprecated?), but it does what I want it to. I was hoping for something more terse, like you can do in bash e.g. var=$(date +"%s")

edit 2

thanks to u/symmitchry I think this is the best choice at the moment.

var = subprocess.check_output('date +"%s"', shell=True)

edit 3

u/Justinsaccount is the winner! just what I wanted in a terse statement.

var = int(time.time())
๐ŸŒ
Python
docs.python.org โ€บ 3 โ€บ library โ€บ subprocess.html
subprocess โ€” Subprocess management
1 week ago - In a multithreaded process, use ... such as os.system(). This also applies to standard handle redirection, which temporarily creates inheritable handles. Added in version 3.7. The subprocess module exposes the following constants. ... The standard input device. Initially, this is the console input buffer, CONIN$. ... The standard output ...
๐ŸŒ
The Coding Forums
thecodingforums.com โ€บ archive โ€บ archive โ€บ python
How do I capture the output of os.system() | Python | Coding Forums
October 22, 2004 - I am trying to capture the output of os.system() into a string without success. Since os.system() returns only its exit_status, how can I do that? system(...) system(command) -> exit_status Execute the command (a string) in a subshell. My aim is to capture the return value of os.system("rsh <netapp_filer_host> qtree") Thanks. -FT ... You should better use os.popen. Please see the documentation of popen in the os module: http://www.python.org/doc/2.3.4/lib/os-newstreams.html#l2h-1380
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ ref_os_system.asp
Python os.system()
This method is implemented by calling the Standard C function system() with some limitations ยท If command generates any output, it is sent to the interpreter standard output stream.The command executed on the respective shell opened by the ...
๐ŸŒ
The Coding Forums
thecodingforums.com โ€บ archive โ€บ archive โ€บ python
os.system and command output | Python | Coding Forums
June 7, 2006 - For example in windows, this command should return a list of files within directory: os.system("DIR") I am looking for an alternative function that returns the DIR command output in a string ยท Click to expand...
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 59269310
python - What does os.system return and how can I convert its output? - Stack Overflow
The os.system() command returns the exit code or status (an integer) of the process, depending on whether your system is Windows or Unix. This is different to what you expect, which is the result of the command zbarimg *.png.
๐ŸŒ
Medium
shivrajan.medium.com โ€บ assign-output-of-os-system-to-a-variable-and-prevent-it-from-being-displayed-on-the-screen-95392dfdd5fd
Assign output of os.system to a variable and prevent it from being displayed on the screen | by Shivrajan Singh | Medium
March 25, 2023 - def out(command): result = run(command, stdout=PIPE, stderr=PIPE, universal_newlines=True, shell=True) return result.stdoutmy_output = out("echo hello world") print(my_output)