Skip to main content
shell=True is unsafe, shorten
Source Link
user3064538
user3064538

Use the subprocess module in the standard librarysubprocess.run:

import subprocess

# for simple commands
subprocess.run(["ls", "-l"]) 

# for complex commands, with many args, use string + `shell=True`:
cmd_str = "ls -l /tmp | awk '{print $3,$9}' | grep root"
subprocess.run(cmd_str, shell=True)

The advantage of subprocess.run overAnother common way is os.system is thatbut you shouldn't use it because it is unsafe if any parts of the command come from outside your program or can contain spaces or other special characters, also subprocess.run is generally more flexible (you can get the stdout, stderr, the "real" status code, better error handling, it is not prone to problems due to spaces in folders in the path to the executable, etc...).

  Even the the documentationdocumentation 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"])

Use the subprocess module in the standard library:

import subprocess

# for simple commands
subprocess.run(["ls", "-l"]) 

# for complex commands, with many args, use string + `shell=True`:
cmd_str = "ls -l /tmp | awk '{print $3,$9}' | grep root"
subprocess.run(cmd_str, shell=True)

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, it is not prone to problems due to spaces in folders in the path to the executable, 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"])

Use subprocess.run:

import subprocess

subprocess.run(["ls", "-l"]) 

Another common way is os.system but you shouldn't use it because it is unsafe if any parts of the command come from outside your program or can contain spaces or other special characters, also subprocess.run is generally 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.

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

subprocess.call(["ls", "-l"])
added 84 characters in body
Source Link
Lasse V. Karlsen
  • 388.1k
  • 103
  • 640
  • 840

Use the subprocess module in the standard library:

import subprocess

# for simple commands
subprocess.run(["ls", "-l"]) 

# for complex commands, with many args, use string + `shell=True`:
cmd_str = "ls -l /tmp | awk '{print $3,$9}' | grep root"
subprocess.run(cmd_str, shell=True)

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, it is not prone to problems due to spaces in folders in the path to the executable, 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"])

Use the subprocess module in the standard library:

import subprocess

# for simple commands
subprocess.run(["ls", "-l"]) 

# for complex commands, with many args, use string + `shell=True`:
cmd_str = "ls -l /tmp | awk '{print $3,$9}' | grep root"
subprocess.run(cmd_str, shell=True)

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"])

Use the subprocess module in the standard library:

import subprocess

# for simple commands
subprocess.run(["ls", "-l"]) 

# for complex commands, with many args, use string + `shell=True`:
cmd_str = "ls -l /tmp | awk '{print $3,$9}' | grep root"
subprocess.run(cmd_str, shell=True)

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, it is not prone to problems due to spaces in folders in the path to the executable, 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"])
a more scalable `run` syntax (single string, many args) is with `shell=True` + non-trivial example
Source Link
mirekphd
  • 6.2k
  • 3
  • 47
  • 73

Use the subprocess module in the standard library:

import subprocess 

# for simple commands
subprocess.run(["ls", "-l"]) 

# for complex commands, with many args, use string + `shell=True`:
cmd_str = "ls -l /tmp | awk '{print $3,$9}' | grep root"
subprocess.run(cmd_str, shell=True)

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"])

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"])

Use the subprocess module in the standard library:

import subprocess 

# for simple commands
subprocess.run(["ls", "-l"]) 

# for complex commands, with many args, use string + `shell=True`:
cmd_str = "ls -l /tmp | awk '{print $3,$9}' | grep root"
subprocess.run(cmd_str, shell=True)

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"])
Rollback to Revision 18
Source Link
Loading
Added more details with demo codes, tips, security implications, and notes for latest python syntax versions such as 3.5+.
Source Link
Loading
Rollback to Revision 16
Source Link
user3064538
user3064538
Loading
Expand the code sample.
Source Link
John Mee
  • 51.5k
  • 38
  • 155
  • 192
Loading
added 405 characters in body
Source Link
user3064538
user3064538
Loading
add version of python needed
Source Link
Corey Goldberg
  • 60.1k
  • 29
  • 132
  • 144
Loading
Improved formatting.
Source Link
martineau
  • 122.4k
  • 29
  • 176
  • 307
Loading
added 96 characters in body
Source Link
FreshPow
  • 7.1k
  • 1
  • 16
  • 17
Loading
upgrade to Python 3. the official documentation says to use run() instead of call(). Remove duplicate link to the documentation
Source Link
Loading
Remove Python 2 - subprocess link because it will retire 2020
Source Link
Loading
Active reading.
Source Link
Peter Mortensen
  • 31.3k
  • 22
  • 109
  • 132
Loading
os.system() does not appear to be currently deprecated, provide different links to python 2/3 docs
Source Link
Loading
removed superfluous comment
Source Link
Corey Goldberg
  • 60.1k
  • 29
  • 132
  • 144
Loading
improved formatting
Source Link
Cristian Ciupitu
  • 20.7k
  • 7
  • 53
  • 78
Loading
corrected link for python 2 version. Note: python site is updated
Source Link
Loading
added 55 characters in body
Source Link
ThiefMaster
  • 316.3k
  • 84
  • 599
  • 642
Loading
edited body
Source Link
Jim Ferrans
  • 30.9k
  • 12
  • 57
  • 83
Loading
Source Link
David Cournapeau
  • 80.1k
  • 9
  • 67
  • 71
Loading