Use the subprocess
module in the standard library:
import subprocess subprocess.run(["ls", "-l"])
The advantage of subprocess.run
over os.system
is that it is more flexible (you can get the stdout
, stderr
, the “real” status code, better error handling, etc…).
Even the documentation for os.system
recommends using subprocess
instead:
The
subprocess
module provides more powerful facilities for spawning new processes and retrieving their results; using that module is preferable to using this function. See the Replacing Older Functions with the subprocess Module section in thesubprocess
documentation for some helpful recipes.
On Python 3.4 and earlier, use subprocess.call
instead of .run
:
subprocess.call(["ls", "-l"])
Related Posts:
- python getoutput() equivalent in subprocess
- Running shell command and capturing the output
- OSError: [WinError 193] %1 is not a valid Win32 application
- What does the Pydoc module do?
- OSError: [Errno 8] Exec format error
- Why is the apt-get function not working in the terminal on Mac OS X v10.9 (Mavericks)?
- Why do people write #!/usr/bin/env python on the first line of a Python script?
- subprocess.check_output return code
- takes 1 positional argument but 2 were given
- How to print colored text to the terminal
- How to print colored text to the terminal
- What can lead to “IOError: [Errno 9] Bad file descriptor” during os.system()?
- Clear terminal in Python [duplicate]
- Using sudo with Python script
- Python Subprocess: Too Many Open Files
- Is there any simple way to benchmark Python script?
- how to check which version of nltk, scikit learn installed?
- are there dictionaries in javascript like python?
- How do I update\upgrade pip itself from inside my virtual environment?
- What does the “yield” keyword do?
- Renaming column names in Pandas
- Does Python have a string ‘contains’ substring method?
- Is there a “not equal” operator in Python?
- IndexError: list index out of range and python
- Python: ‘ModuleNotFoundError’ when trying to import module from imported package
- Check if a given key already exists in a dictionary
- ValueError: setting an array element with a sequence
- ‘python’ is not recognized as an internal or external command [duplicate]
- Importing modules from parent folder
- Should I use np.absolute or np.abs?
- Writing a list to a file with Python
- Why am I getting “LinAlgError: Singular matrix” from grangercausalitytests?
- if else in a list comprehension
- How can I make a Python script standalone executable to run without ANY dependency? [duplicate]
- python exception message capturing
- What is the best project structure for a Python application?
- In Matplotlib, what does the argument mean in fig.add_subplot(111)?
- Convert a python dict to a string and back
- sys.argv[1] meaning in script
- TypeError: Object of type ‘bytes’ is not JSON serializable
- How to initialize a two-dimensional array in Python?
- How to unpack pkl file?
- What does numpy.gradient do?
- Is there a Python equivalent to the ‘which’ command
- How to clear the interpreter console?
- Get unique values from a list in python
- ‘list’ object has no attribute ‘shape’
- What does the ‘b’ character do in front of a string literal?
- How to add multiple values per key in python dictionary
- Pythonic way to combine FOR loop and IF statement
- Python: OverflowError: math range error
- Best way to replace multiple characters in a string?
- Most efficient way to reverse a numpy array
- Python return list from function
- Remove all special characters, punctuation and spaces from string
- How to delete last item in list?
- How to configure logging to syslog in Python?
- Converting binary to decimal integer output
- Don’t understand this SyntaxError: illegal target for annotation
- No module named setuptools
- Using Queue in python
- Whats the difference between ‘for x in list:’ and ‘for x in list[:]:’
- ctypes error: libdc1394 error: Failed to initialize libdc1394
- Python: cannot concatenate ‘str’ and ‘int’ objects error
- How to “test” NoneType in python?
- AttributeError:’bytes’ object has no attribute ‘encode’
- python mpl_toolkits installation issue
- What does bitwise_and operator exactly do in openCV?
- RuntimeWarning: numpy.dtype size changed, may indicate binary incompatibility
- Using BeautifulSoup to search HTML for string
- TensorFlow: Blas GEMM launch failed
- Python Weather API
- How do I get time of a Python program’s execution?
- Pandas DataFrame: replace all values in a column, based on condition
- How to create a trie in Python
- How to start from second index for for-loop
- Python: How to check if keys exists and retrieve value from Dictionary in descending priority
- Python: Converting string into decimal number
- Importing Pandas gives error AttributeError: module ‘pandas’ has no attribute ‘core’ in iPython Notebook
- What does this Django regular expression mean? `?P`
- Strange error about invalid syntax
- ‘virtualenv’ is not recognized as an internal or external command, operable program or batch file
- Python Coin Toss
- Is it better to use path() or url() in urls.py for django 2.0?
- gunicorn.errors.HaltServer:
django - What is a clean, Pythonic way to have multiple constructors in Python?
- Python Login Script; Usernames and Passwords in a separate file
- Ignore .pyc files in git repository
- MovieWriter ffmpeg unavailable; trying to use class ‘matplotlib.animation.PillowWriter’ instead
- How can I access global variable inside class in Python
- Sorting arrays in NumPy by column
- How to open html file?
- In Python, is there an elegant way to print a list in a custom format without explicit looping?
- Python 2.7 mixing iteration and read methods would lose data
- Conditional indexing with Numpy ndarray
- python numpy machine epsilon
- Installing lxml module in python
- Extracting specific selected columns to new DataFrame as a copy
- How to read a CSV file from a URL with Python?
- How to print both strings in a dictionary in Python