使用python发出命令提示符 [英] Using python to issue command prompts
问题描述
在过去几个月里我一直在教自己的python,最后开始做一些有用的事情。
I have been teaching myself python over the past few months and am finally starting to do some useful things.
我想要做的最好是有一个python脚本作为队列。也就是说,我想有一个文件夹,一个输入文件,另一个程序用来运行计算(我是一个理论物理学家,每天做许多计算工作)。
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).
我现在必须这样做的方式是将所有的输入文件放在具有计算软件的盒子上。然后我必须将dos输入文件转换为unix(dos2unix),下面我必须将新的输入文件复制到一个名为'INPUT'的文件。最后,我运行一个命令来启动该作业。
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.
所有这些任务都在命令提示符中处理。我的问题是如何界面我的程序与命令提示符?然后,我如何监视进程(我通常通过cpu使用和TOP命令做),并且让python在最后一个作业完成后立即启动下一个作业。
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
推荐答案
subprocess 模块有许多用于在python中执行系统命令的工具。
The subprocess module has many tools for executing system commands in python.
from subprocess import call
call(["ls", "-l"])
调用将等待命令完成并返回其返回码,因此您可以调用
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
是一种旧版本的方法,但是建议使用的工具较少:
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
这篇关于使用python发出命令提示符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!