【问题标题】:Executing the same batch file (.bat) using python使用python执行相同的批处理文件(.bat)
【发布时间】:2021-05-31 17:47:19
【问题描述】:

我开发了以下循环,用于使用循环运行具有不同降雨集合的模型。除了我的.bat文件的执行过程之外,脚本运行正常。

import os
import glob
import subprocess

ws = (r'C:\Users\Wahid Hussainzada\Desktop\Takahashi_2018_9_test')
os.chdir(ws)
myFile = glob.glob('*.txt')
myModel = glob.glob('2cdrmv3.bat')


for i in myFile:
    if i.startswith('rain'):
        print(i)
        myBat = open(ws+'\wahid.bat','w+')
        myBat.write('cdrmv3.exe param1.txt param2.txt param3.txt param4.txt bsndir.txt slope.txt length.txt order.txt takahashi_landuse.txt takahashi_acc.txt 0 # 1 takahashi_thiessen.txt 13 '+str(i) +' 0 1 out_'+str(i) +' 0 outgrid\outa2 outgrid\outb')
        myBat.close()
        subprocess.call('C:\Users\Wahid Hussainzada\Desktop\Takahashi_2018_9_test\wahid.bat')
    else:
        print("Other txt files than rain files")
            

【问题讨论】:

标签: python loops batch-file spyder modeling


【解决方案1】:

尝试使用字符串字面量

subprocess.call(r'C:\Users\Wahid Hussainzada\Desktop\Takahashi_2018_9_test\wahid.bat')

你可能需要做类似的事情

cmd = r'C:\Windows\System32\cmd.exe'
script = r'C:\Users\Wahid Hussainzada\Desktop\Takahashi_2018_9_test\wahid.bat'
subprocess.call([cmd,script])

【讨论】:

  • 感谢您的回复。我正在使用 Spyder 来执行代码。我在控制台中看不到.bat文件执行的过程。你能帮我解决这个问题吗?
  • 我使用了代码,它没有收到任何错误。它运行了很长时间,没有输出。
【解决方案2】:

根据@Joran Beasley 的回答,代码可以进行如下小修改:

cmd = r'C:\Windows\System32\cmd.exe'
script = r'Working directory\*.bat'
subprocess.call([script,cmd])

【讨论】:

    猜你喜欢
    • 2016-08-31
    • 1970-01-01
    • 1970-01-01
    • 2019-08-29
    • 1970-01-01
    • 1970-01-01
    • 2011-09-30
    • 1970-01-01
    • 2015-01-06
    相关资源
    最近更新 更多