Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using python to issue command prompts

I have been teaching myself python over the past few months and am finally starting to do some useful things.

What I am trying to ultimately do is have a python script that acts as a queue. That is, I would like to have a folder with a bunch of input files that another program uses to run calculations (I am a theoretical physicist and do many computational jobs a day).

The way I must do this now is put all of the input files on the box that has the computational software. Then I have to convert the dos input files to unix (dos2unix), following this I must copy the new input file to a file called 'INPUT'. Finally I run a command that starts the job.

All of these tasks are handled in a command prompt. My question is how to I interface my program with the command prompt? Then, how can I monitor the process (which I normally do via cpu usage and the TOP command), and have python start the next job as soon as the last job finishes.

Sorry for rambling, I just do not know how to control a command prompt from a script, and then have it automatically 'watch' the job.

Thanks

like image 462
Michael R Avatar asked Jul 10 '13 19:07

Michael R


People also ask

How do I run multiple command prompts in Python?

If you want to execute many commands one after the other in the same session/shell, you must start a shell and feed it with all the commands, one at a time followed by a new line, and close the pipe at the end.

How do I get Python to recognize a command prompt?

One way to fix the error would be to launch Python from the Command Prompt by passing in the full path to the executable file each time you wanted to run Python. In other words, instead of typing Python you would type something like C:\Users\me\path\to\python.exe .


2 Answers

The subprocess module has many tools for executing system commands in python.

from subprocess import call
call(["ls", "-l"])

source

call will wait for the command to finish and return its returncode, so you can call another one afterwards knowing that the previous one has finished.

os.system is an older way to do it, but has fewer tools and isn't recommended:

import os
os.system('"C:/Temp/a b c/Notepad.exe"')

edit FvD left a comment explaning how to "watch" the process below

like image 87
Stephan Avatar answered Oct 14 '22 20:10

Stephan


If you actually need to drive an interactive command-line interface, there is no way to do that with the stdlib.

There are a number of third-party options for this; I think pexpect is probably the most popular.


However, if you don't really need to drive it interactively—if the program only needs you to give it arguments on the command line, or a "batch mode" dump to its standard input, then subprocess makes it easy. For example, to drive the sort program, you can just do this:

with Popen(['sort', '-n'], stdin=PIPE, stdout=PIPE) as p:
    sorted_data = p.communicate(input_data)

This is of course a silly example, because you can do anything sort can do with Python's sorted with a key argument, and probably a lot more readably.


More generally: often when you think you need to interactively script some program, you really don't, and sometimes you don't even need to run it at all.

And your particular case is exactly such a case. You're asking about interactively scripting the shell. But you don't need actually need to do so, because Python can do all the things you need from the shell.

like image 38
abarnert Avatar answered Oct 14 '22 21:10

abarnert