Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python run a system command

import os
cmd = "git --version"
returned_value = os.system(cmd)  # returns the exit code in unix
Comment

python - How to execute a program or call a system command?

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 the subprocess documentation for some helpful recipes.

On Python 3.4 and earlier, use subprocess.call instead of .run:

subprocess.call(["ls", "-l"]) 
Comment

PREVIOUS NEXT
Code Example
Python :: pandas.DataFrame.fillna 
Python :: logical operators in python 
Python :: pandas read csv specify column dtype 
Python :: tkinter hide widget 
Python :: sys.maxsize() in python 
Python :: sorted key python 
Python :: anaconda python 3.6 download 
Python :: print to screen 
Python :: How to get historical klines python binance 
Python :: list functions 
Python :: how to use a for loop in python 
Python :: how to change order of attributes of an element using beautiful soup 
Python :: python given upper triangle construct symmetric matrix 
Python :: tkinter how to update optionmenu contents 
Python :: complete dates pandas 
Python :: required_fields = [] 
Python :: change a color on touch roblox 
Python :: python count one to ten 
Python :: find occerences in list python 
Python :: pyspark parquet to dataframe 
Python :: get all functions from a module as string list python 
Python :: python using strip trim white sapce 
Python :: how to sort in python 
Python :: matplotlib ax.annotate color of the arrow 
Python :: pil saves blue images 
Python :: python defualt error handler 
Python :: add text in figure coordinatesp ython 
Python :: humanname python 
Python :: python socket github 
Python :: save model with best validation loss keras 
ADD CONTENT
Topic
Content
Source link
Name
7+9 =