简体   繁体   English

从python调用时如何等待批处理文件结束?

[英]How to wait for batch file to end when called from python?

I have a batch file which is zipping a folder as follows(zip.bat):我有一个批处理文件,它正在压缩一个文件夹,如下所示(zip.bat):

for /d %%X in (D:/sample/target/bin) do "c:\Program Files\7-Zip\7z.exe" a -mx "%%X.zip" "%%X\*"

The batch file will zip folder bin and works correctly.批处理文件将压缩文件夹 bin 并正常工作。 Now am calling this batch from a python script as follows:现在我从 python 脚本调用这个批处理,如下所示:

import subprocess as sp
import sys
start_zip_batch = sp.Popen(['D:/zip.bat'],stdin=sp.PIPE, stdout=sp.PIPE, stderr=sp.PIPE, shell = True)
start_zip_batch.wait()

So, now when the script is made to execute it creates the bin.zip folder but keep showing 0 bytes inside it and after a while it stops responding and i need to manually close it.所以,现在当脚本被执行时,它会创建 bin.zip 文件夹,但在其中继续显示 0 字节,一段时间后它停止响应,我需要手动关闭它。 The bin folder is around 403 mb. bin 文件夹大约为 403 mb。 So, please suggest how shall i call zip.bat from python such that it creates te zip folder and then gets closed neatly.所以,请建议我如何从 python 调用 zip.bat 以便它创建 te zip 文件夹,然后整齐地关闭。

There are several subprocess methods that wait for completion.有几个子流程方法等待完成。

check_call is useful if you want to do cleanup or reporting based on the command return code.如果您想根据命令返回码进行清理或报告,则check_call很有用。

subprocess.check_call(args, *, stdin=None, stdout=None, stderr=None, shell=False) subprocess.check_call(args, *, stdin=None, stdout=None, stderr=None, shell=False)

Run command with arguments.运行带参数的命令。 Wait for command to complete.等待命令完成。 If the return code was zero then return, otherwise raise CalledProcessError.如果返回码为零则返回,否则引发 CalledProcessError。 The CalledProcessError object will have the return code in the returncode attribute. CalledProcessError 对象将在 returncode 属性中包含返回代码。

The Docs文档

print('filename')

如果您在.bat文件中没有任何输出,则可以使用

The easiest and most modern way is to use subprocess.run() which executes the process and waits for it to complete:最简单和最现代的方法是使用subprocess.run()来执行进程并等待它完成:

import subprocess
p = subprocess.run(['D:/zip.bat'])

This will execute the process and return a CompletedProcess object for which you can inspect the returncode attribute to determine that status.这将执行流程并返回一个CompletedProcess对象,您可以检查该对象的returncode属性以确定该状态。

The process's stdout and stderr destinations are unchanged, eg output will still appear on the terminal if the command is run from a terminal.进程的 stdout 和 stderr 目标没有改变,例如,如果命令是从终端运行的,则输出仍会出现在终端上。

You can capture the output of both stdout and stderr if you want:如果需要,您可以捕获 stdout 和 stderr 的输出:

p = subprocess.run(['D:/zip.bat'], capture_output=True)
print(p.stdout.decode())
print(p.stderr.decode())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 已调用Python脚本,但未从批处理文件执行 - Python script is called but not executed from batch file 如何将在Python脚本中调用的批处理文件的结果写入文件 - How to write the results of a batch file called in a Python script to a file 在“if”和“timeout”之后从批处理调用时,Python输入被阻止 - Python input blocked when called from batch within an “if” and after a “timeout” 如何让 Python 不等待命令结束? - How to make Python not to wait for the end of the command? 当默认为Python 2时,尝试从批处理文件调用Python 3脚本 - Trying to call a Python 3 script from a batch file when Python 2 is the default 从批处理文件运行 python 脚本时出现 ModuleNotFoundError - ModuleNotFoundError when running python script from a batch file Python结束线程,当函数从结束处调用并从线程传递异常时 - Python end thread when function it's called from ends and pass up exception from thread 运行批处理程序时从python文件加载变量失败 - Loading variables from python file failed when running batch program 当不同用户调用相同的 function 超过 10 次时,如何使用 python 读取和写入文件 - how to read and write a file with python when same function is called >10 times from different users 批量运行python脚本,并等待其执行返回 - run python scripts in batch and wait for return from their executions
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM