用命令提示符python,使用python发出命令提示符

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

解决方案

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 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值