You can use subprocess.call, with the stdout keyword argument:

import subprocess

cmd = ['ls', '-l']

with open('output.txt', 'w') as out:
    return_code = subprocess.call(cmd, stdout=out)
Answer from Warren Weckesser on Stack Overflow
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ write-os-system-output-in-file-using-python
Write Os.System Output In File Using Python - GeeksforGeeks
July 23, 2025 - The below Python code uses os.system() to execute the "dir" command, redirecting its output to a file named "output.txt" using the ">" operator, and prints either "Successfully executed" or "Failed" based on the exit code (0 for success).
Discussions

how to get the stdout of the script that os.system executes - Post.Byes
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 ">". More on post.bytes.com
๐ŸŒ post.bytes.com
October 31, 2007
linux - reading output into variables inside the os.system command - Unix & Linux Stack Exchange
Suggest me on how to store the variables using os.system command how to use them in the later stages. My target here is to print all the variables including the time being captured into a output file outputfile.txt. TIA ... <(...) is ksh syntax also recognised by zsh and bash, though when used as the target of a redirection, it's only supported by zsh and bash. In any case, that is not sh syntax. Python... More on unix.stackexchange.com
๐ŸŒ unix.stackexchange.com
February 8, 2018
How can I put all outputs that would appear in the terminal into a text file?
u mean in bash or python ? More on reddit.com
๐ŸŒ r/learnpython
9
1
January 26, 2022
python - Save output of os.system to text file - Stack Overflow
I'm not great on all the technical terms so I'll do my best to explain my problem. I've written a small script to open android SDK and check for attached devices (using windows 10 and python 2.7.1... More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ print-output-from-os-system-in-python
Print Output from Os.System in Python - GeeksforGeeks
July 23, 2025 - Output from command: Hello, World! Example 2: In this Python example, the os.system function is used to run a command (ls -l to list files in the current directory). The exit code of the command is captured, and it is printed to the console.
๐ŸŒ
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 - import os fin, fout = os.popen4(cmd) print fout.read() #standard out commands is a simple unix specific wrapper for os.popen: The whole content of commands.py: [code=python] smygis@Bob:~$ cat /usr/lib/python2.5/commands.py """Execute shell commands via os.popen() and return status, output. Interface summary: import commands outtext = commands.getout put(cmd) (exitstatus, outtext) = commands.getsta tusoutput(cmd) outtext = commands.getsta tus(file) # returns output of "ls -ld file" A trailing newline is removed from the output string.
๐ŸŒ
DigitalOcean
digitalocean.com โ€บ community โ€บ tutorials โ€บ python-system-command-os-subprocess-call
Python os.system() vs subprocess: Run System Commands | DigitalOcean
March 17, 2026 - It lets you interact with a running ... stream output line by line, and manage stdin/stdout/stderr pipes directly. 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...
๐ŸŒ
LinuxQuestions.org
linuxquestions.org โ€บ questions โ€บ programming-9 โ€บ redirect-stdout-of-os-system-420796
redirect stdout of os.system()?
March 1, 2006 - How to redirect the stdout of "os.system(), os.execlp() or os.spawnlp() to a file or List(one line, one item)? Code: 1 #!/usr/bin/python 2 # -*-
Find elsewhere
๐ŸŒ
Ask Ubuntu
askubuntu.com โ€บ questions โ€บ 997431 โ€บ how-to-save-text-file-command-line-using-os-system
python - How to save text file command line using os.system? - Ask Ubuntu
January 18, 2018 - import os os.chdir("PATH") os.chdir("tensor") os.system(". bin/activate") os.system("tensorflow/bazel-bin/tensorflow/examples/label_image/label_image --input_layer=Mul --output_layer=final_result --labels=PATH --image=PATH --graph=PATH") python3 Predict.py make &> PATH ยท Not Commandline just write text file ยท
๐ŸŒ
Python
docs.python.org โ€บ 3 โ€บ library โ€บ subprocess.html
subprocess โ€” Subprocess management
1 week ago - If encoding or errors are specified, or text (also known as universal_newlines) is true, the file objects stdin, stdout and stderr will be opened in text mode using the encoding and errors specified in the call or the defaults for io.TextIOWrapper. For stdin, line ending characters '\n' in the input will be converted to the default line separator os.linesep. For stdout and stderr, all line endings in the output will be converted to '\n'. For more information see the documentation of the io.TextIOWrapper class when the newline argument to its constructor is None.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ ways-to-save-python-terminal-output-to-a-text-file
Ways To Save Python Terminal Output To A Text File - GeeksforGeeks
July 23, 2025 - In Python, you can redirect the standard output (which normally goes to the terminal) to a file using sys.stdout.
๐ŸŒ
LearnByExample
learnbyexample.github.io โ€บ 100_page_python_intro โ€บ executing-external-commands.html
Executing external commands - 100 Page Python Intro
This chapter will show how to execute external commands from Python, capture their output and other relevant details such as the exit status. The availability of commands depends on the OS you are using (mine is Linux). Last chapter showed a few examples with the os module for file processing. This module is useful in plenty of other situations as well โ€” for example, providing an interface for working with external commands. >>> import os >>> os.system('echo hello "$USER"') hello learnbyexample 0
๐ŸŒ
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
๐ŸŒ
Python
python-list.python.narkive.com โ€บ rOvOpkOz โ€บ redirect-os-system-output
Redirect os.system output
Just install cgywin (including python) then follow the standard instructions for pexpect. There was one small trick I had to do to get cygwin working totally properly on my machine which was run a rebaseall. Rebaseall sets the memory addresses for the DLLs or something like that. However, there is a slight problem. The rebaseall runs inside cygwin and uses one of the DLLs. To get around this I change the rebaseall script to write it's command to a text file and then run those commands in a DOS cmd shell.
๐ŸŒ
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...
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ how can i put all outputs that would appear in the terminal into a text file?
r/learnpython on Reddit: How can I put all outputs that would appear in the terminal into a text file?
January 26, 2022 -

I have a script running that prints a couple of lines then calls a different python function in a different directory which also prints something. This repeats about a 100 times.

Now I would like to direct all outputs of the terminal to a text file.

I've tried changing sys.stdout to an opened text file but that did not work.

Typing > file.txt and nohup Both resulted in having all the prints from the inner function first, then all the prints from the outer function.

I could just run it in the terminal but there has to be a better solution. Does anybody have an idea?

๐ŸŒ
Python
bugs.python.org โ€บ issue11820
Issue 11820: idle3 shell os.system swallows shell command output - Python tracker
April 10, 2011 - This issue tracker has been migrated to GitHub, and is currently read-only. For more information, see the GitHub FAQs in the Python's Developer Guide ยท This issue has been migrated to GitHub: https://github.com/python/cpython/issues/56029
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ python os.system() method
Python os.system() Method - Scaler Topics
January 1, 2024 - If successful, the file will be downloaded and saved in the current directory. The os.system() method in Python is used for the execution of shell commands directly from a Python script. It takes command as a parameter which specifies the shell command to be executed. The command does not return the command output but instead provides an exit code indicating the command's status.