Skip to main content
Second iteration. Fixed the weird syntax highlighting (as a result, the diff looks more extensive than it really is - use view "Side-by-side markdown" to compare). Removed meta information (this belongs in comments or in the edit summary).
Source Link
Peter Mortensen
  • 31.3k
  • 22
  • 109
  • 132

Sources:

Sources

These are all the libraries:

These are all the libraries

subprocess

subprocess

os

os

os.system("ls -l") # run command
os.popen("ls -l").read() # This will run the command and return any output
os.system("ls -l") # Run command
os.popen("ls -l").read() # This will run the command and return any output

sh

sh

plumbum

plumbum

ls_cmd = plumbum.local("ls -l") # getGet command
ls_cmd() # runRun command

pexpect

pexpect

fabric

fabric

envoy

envoy

r = envoy.run("ls -l") # Run command
r.std_out # getGet output

commands

commands

commands contains wrapper functions for os.popen, but it has been removed from Python 3 since subprocess is a better alternative.

The edit was based on J.F. Sebastian's comment.

Sources:

These are all the libraries:

subprocess

os

os.system("ls -l") # run command
os.popen("ls -l").read() # This will run the command and return any output

sh

plumbum

ls_cmd = plumbum.local("ls -l") # get command
ls_cmd() # run command

pexpect

fabric

envoy

r = envoy.run("ls -l") # Run command
r.std_out # get output

commands

commands contains wrapper functions for os.popen, but it has been removed from Python 3 since subprocess is a better alternative.

The edit was based on J.F. Sebastian's comment.

Sources

These are all the libraries

subprocess

os

os.system("ls -l") # Run command
os.popen("ls -l").read() # This will run the command and return any output

sh

plumbum

ls_cmd = plumbum.local("ls -l") # Get command
ls_cmd() # Run command

pexpect

fabric

envoy

r = envoy.run("ls -l") # Run command
r.std_out # Get output

commands

commands contains wrapper functions for os.popen, but it has been removed from Python 3 since subprocess is a better alternative.

Active reading. [<http://en.wikipedia.org/wiki/Python_%28programming_language%29> <http://en.wikipedia.org/wiki/Unix>]
Source Link
Peter Mortensen
  • 31.3k
  • 22
  • 109
  • 132

There are lots of different libraries which allow you to call external commands with pythonPython. For each library I've given a description and shown an example of calling an external command, the. The command I used as the example is ls -l (list all files). If you want to find out more about any of the libraries I've listed and linked the documentation for each of them.

Subprocess allows you to call external commands and connect them to their input/output/error pipes (stdin, stdout, and stderr). Subprocess is the default choice for running commands, but sometimes other modules are better.

subprocess.run(["ls", "-l"]) # runRun command
subprocess.run(["ls", "-l"], stdout=subprocess.PIPE) # thisThis will run the command and return any output
subprocess.run(shlex.split("ls -l")) # youYou can also use the shlex library to split the command
os.system("ls -l") # run command
os.popen("ls -l").read() # thisThis will run the command and return any output

sh is a subprocess interface which lets you call programs as if they were functions, this. This is useful if you want to run a command multiple times.

sh.ls("-l") # runRun command normally
ls_cmd = sh.Command("ls") # saveSave command as a variable
ls_cmd() # runRun command as if it were a function

plumbum is a library for "script-like" pythonPython programs. You can call programs like functions as in shsh. Plumbum is useful if you want to run a pipeline without the shell.

pexpect lets you spawn child applications, control them and find patterns in their output. This is a better alternative to subprocess for commands that expect a tty on unixUnix.

pexpect.run("ls -l") # runRun command as normal
child = pexpect.spawn('scp foo [email protected]:.') # spawnsSpawns child application
child.expect('Password:') # whenWhen this is the output
child.sendline('mypassword')

fabric is a Python 2.5 and 2.7 library, it. It allows you to execute local and remote shell commands. Fabric is simple alternative for running commands in a secure shell (SSH)

fabric.operations.local('ls -l') # runRun command as normal
fabric.operations.local('ls -l', capture = True) # runRun command and receive output

envoy is known as "subprocess for humans", it. It is used as a convenience wrapper around the subprocess module.

r = envoy.run("ls -l") # runRun command
r.std_out # get output

commandscommands contains wrapper functions for os.popen, but it has been removed from Python 3 since subprocess is a better alternative.

[EDIT] BasedThe edit was based on J.F. Sebastian's comment.

There are lots of different libraries which allow you to call external commands with python. For each library I've given a description and shown an example of calling an external command, the command I used as the example is ls -l (list all files). If you want to find out more about any of the libraries I've listed and linked the documentation for each of them.

Subprocess allows you to call external commands and connect them to their input/output/error pipes (stdin, stdout and stderr). Subprocess is the default choice for running commands, but sometimes other modules are better.

subprocess.run(["ls", "-l"]) # run command
subprocess.run(["ls", "-l"], stdout=subprocess.PIPE) # this will run the command and return any output
subprocess.run(shlex.split("ls -l")) # you can also use the shlex library to split the command
os.system("ls -l") # run command
os.popen("ls -l").read() # this will run the command and return any output

sh is a subprocess interface which lets you call programs as if they were functions, this is useful if you want to run a command multiple times.

sh.ls("-l") # run command normally
ls_cmd = sh.Command("ls") # save command as a variable
ls_cmd() # run command as if it were a function

plumbum is a library for "script-like" python programs. You can call programs like functions as in sh. Plumbum is useful if you want to run a pipeline without the shell.

pexpect lets you spawn child applications, control them and find patterns in their output. This is a better alternative to subprocess for commands that expect a tty on unix.

pexpect.run("ls -l") # run command as normal
child = pexpect.spawn('scp foo [email protected]:.') # spawns child application
child.expect('Password:') # when this is the output
child.sendline('mypassword')

fabric is a Python 2.5 and 2.7 library, it allows you to execute local and remote shell commands. Fabric is simple alternative for running commands in a secure shell (SSH)

fabric.operations.local('ls -l') # run command as normal
fabric.operations.local('ls -l', capture = True) # run command and receive output

envoy is known as "subprocess for humans", it is used as a convenience wrapper around the subprocess module.

r = envoy.run("ls -l") # run command
r.std_out # get output

commands contains wrapper functions for os.popen but has been removed from Python 3 since subprocess is a better alternative

[EDIT] Based on J.F. Sebastian's comment

There are lots of different libraries which allow you to call external commands with Python. For each library I've given a description and shown an example of calling an external command. The command I used as the example is ls -l (list all files). If you want to find out more about any of the libraries I've listed and linked the documentation for each of them.

Subprocess allows you to call external commands and connect them to their input/output/error pipes (stdin, stdout, and stderr). Subprocess is the default choice for running commands, but sometimes other modules are better.

subprocess.run(["ls", "-l"]) # Run command
subprocess.run(["ls", "-l"], stdout=subprocess.PIPE) # This will run the command and return any output
subprocess.run(shlex.split("ls -l")) # You can also use the shlex library to split the command
os.system("ls -l") # run command
os.popen("ls -l").read() # This will run the command and return any output

sh is a subprocess interface which lets you call programs as if they were functions. This is useful if you want to run a command multiple times.

sh.ls("-l") # Run command normally
ls_cmd = sh.Command("ls") # Save command as a variable
ls_cmd() # Run command as if it were a function

plumbum is a library for "script-like" Python programs. You can call programs like functions as in sh. Plumbum is useful if you want to run a pipeline without the shell.

pexpect lets you spawn child applications, control them and find patterns in their output. This is a better alternative to subprocess for commands that expect a tty on Unix.

pexpect.run("ls -l") # Run command as normal
child = pexpect.spawn('scp foo [email protected]:.') # Spawns child application
child.expect('Password:') # When this is the output
child.sendline('mypassword')

fabric is a Python 2.5 and 2.7 library. It allows you to execute local and remote shell commands. Fabric is simple alternative for running commands in a secure shell (SSH)

fabric.operations.local('ls -l') # Run command as normal
fabric.operations.local('ls -l', capture = True) # Run command and receive output

envoy is known as "subprocess for humans". It is used as a convenience wrapper around the subprocess module.

r = envoy.run("ls -l") # Run command
r.std_out # get output

commands contains wrapper functions for os.popen, but it has been removed from Python 3 since subprocess is a better alternative.

The edit was based on J.F. Sebastian's comment.

removed question :)
Source Link
Tom Fuller
  • 5.3k
  • 7
  • 33
  • 42

Did I miss any? Add a comment or edit my post :)

Did I miss any? Add a comment or edit my post :)

use of os module
Source Link
Tom Fuller
  • 5.3k
  • 7
  • 33
  • 42
Loading
added when and why each module is useful
Source Link
Tom Fuller
  • 5.3k
  • 7
  • 33
  • 42
Loading
encouraging people to edit if necessary
Source Link
Tom Fuller
  • 5.3k
  • 7
  • 33
  • 42
Loading
moved sources to the top
Source Link
Tom Fuller
  • 5.3k
  • 7
  • 33
  • 42
Loading
Source Link
Tom Fuller
  • 5.3k
  • 7
  • 33
  • 42
Loading