user2315
user2315

Reputation: 831

Running command lines within your Python script

So I have a bunch of aliases and Command Line prompt programs, and my main program works by inputting b into the cmd.exe, followed by some filepath names and what not. How would I run those arguments in my python script? So that it mimics the action i am doing in the cmd?

Upvotes: 19

Views: 88615

Answers (4)

Piotr Dobrogost
Piotr Dobrogost

Reputation: 42415

Check out Sarge - a wrapper for subprocess which aims to make life easier for anyone who needs to interact with external applications from their Python code. and Plumbum - a small yet feature-rich library for shell script-like programs in Python.

Upvotes: 2

Infinite_Loop
Infinite_Loop

Reputation: 380

or you can use

import os
os.system('your_command')

for example:

import os
os.system('notepad')

will launch the notepad with the command line behind.

hope this helps

Upvotes: 15

Jakob Bowyer
Jakob Bowyer

Reputation: 34688

You can do this using subprocess

For example, this call bellow gets the output of the program and stores it as a string, using .call will help with calling it and for more accurate control use .Popen

subprocess.check_output(["ipconfig"])

Upvotes: 2

murgatroid99
murgatroid99

Reputation: 20232

You should use the subprocess module. In particular, subprocess.call will run command line programs for you.

Upvotes: 20

Related Questions