6

I am new to Python programming. I want to execute a shell command "at" from a Python program. Can any one of the Python gurus help me out? Thanks in advance.

gomad
  • 1,029
  • 7
  • 16
hue
  • 1,759
  • 3
  • 27
  • 37

4 Answers4

8

The subprocess module can be used for this purpose:

import subprocess
retcode = subprocess.call(["at", "x", "y", "z"])

Replace x, y and z with the parameters to at.

Sven Marnach
  • 574,206
  • 118
  • 941
  • 841
4

Alternatively, rather than using subprocess.call(), you could use Popen to capture the output of the command (rather than the return code).

import subprocess
process = subprocess.Popen(['at','x','y','z'], stdout=subprocess.PIPE).communicate()[0]

This may not be relevant to the at command, but it is useful to know. The same can be done with the stdin and stderr. Look at the documentation for more details.

Michael Smith
  • 1,847
  • 2
  • 19
  • 19
3

use search. already answered: How do I execute a program from python? os.system fails due to spaces in path

Community
  • 1
  • 1
MechanisM
  • 906
  • 8
  • 14
  • However, du to this question being asked years later, the solutions differ. Additionally, this question is not about spaces and possibly not about Windows. – JonnyJD Dec 19 '12 at 11:34
1

subprocess.check_output appears to be the canonical convenience function in Python 2.4+ for executing a command and inspecting the output. It also raises an error if the command returns a non-zero value (indicating an error).

Like subprocess.call, check_output is a convenience wrapper around subprocess.Popen, so you may prefer using Popen directly. But convenience wrappers are ...convenient, in theory.

Vynce
  • 2,947
  • 2
  • 15
  • 12