19

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?

2
  • 1
    Can you give some examples? cmd.exe is this under Windows, talk of aliases made me think it was Linux/Unix. Commented May 24, 2012 at 16:30
  • Yes this is Windows. For example I would run a command "b Y TUP TUP010" b being the program, Y the drive, TUP the directory, and TUP010 the subdirectory Commented May 24, 2012 at 16:33

4 Answers 4

20

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

Sign up to request clarification or add additional context in comments.

Comments

15

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

3 Comments

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.
I've set up an alias so that when I type maya it opens an animation software maya. Yet when I do this, it works for notepad, but not for maya.
I agree subprocess is way better.
2

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

Comments

2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.