Python 2.7 如何使用CMD递归运行*.exe?

Python 2.7 如何使用CMD递归运行*.exe?,python-2.7,cmd,Python 2.7,Cmd,我有一个可执行文件“nc2text.exe”,我正在通过cmd和一组选项运行它。例如: nc2text.exe ifile.nc temperature > ofile.txt 我不得不重复这个步骤很多次,我想知道我是否可以使用cmd或python脚本来自动化它。有什么建议吗 Python: import time import subprocess i = 0 def periodicTimer(): subprocess.call("nc2text.exe ifile" +

我有一个可执行文件“nc2text.exe”,我正在通过cmd和一组选项运行它。例如:

nc2text.exe ifile.nc temperature > ofile.txt

我不得不重复这个步骤很多次,我想知道我是否可以使用cmd或python脚本来自动化它。有什么建议吗

Python:

import time
import subprocess

i = 0
def periodicTimer():
    subprocess.call("nc2text.exe ifile" + str(i) + ".nc temperature > ofile" + str(i) + ".txt")
    i = i+1
    time.sleep(60)

while True:
    periodicTimer()
@echo off
cls
Set counter=0
:start
nc2text.exe ifile%counter%.nc temperature > ofile%counter%.txt
Set /A counter+=1
echo %counter%
timeout 5 /NOBREAK
goto start
:end
pause
我的计算机上没有
nc2text.exe
,但将
子进程
替换为
打印

print "nc2text.exe ifile" + str(i) + ".nc temperature > ofile" + str(i) + ".txt"
产出:

nc2text.exe ifile0.nc temperature > ofile0.txt
nc2text.exe ifile1.nc temperature > ofile1.txt
nc2text.exe ifile2.nc temperature > ofile2.txt
nc2text.exe ifile3.nc temperature > ofile3.txt
等等

批处理:

import time
import subprocess

i = 0
def periodicTimer():
    subprocess.call("nc2text.exe ifile" + str(i) + ".nc temperature > ofile" + str(i) + ".txt")
    i = i+1
    time.sleep(60)

while True:
    periodicTimer()
@echo off
cls
Set counter=0
:start
nc2text.exe ifile%counter%.nc temperature > ofile%counter%.txt
Set /A counter+=1
echo %counter%
timeout 5 /NOBREAK
goto start
:end
pause

您的意思是多次运行完全相同的命令?是的,但输入和输出文件名将更改。输入将为ifile1、ifile2。。。。和输出文件名将类似于我不熟悉在windows上运行批处理。那么,我是否需要将上述批处理程序保存在一个文件中并通过cmd或其他方式运行?在记事本
example.bat
中创建一个批处理文件,将代码段复制粘贴到那里,保存,复制到
nc2text.exe
所在的位置,然后从
cmd
运行它,并将
@echo off
更改为
@echo on
,它将帮助您调试。批处理选项已成功运行。但是,以下一个文件测试失败:subprocess.call(“nc2text.exe infle.nc temperature>test.txt”)time.sleep(30)它启动cmd,但奇怪地没有返回任何内容(没有错误消息)。