让python等待subprocess.call完成其命令

问题描述 投票:0回答:3

所以这个问题从this开始继续(阅读注释以及我所走的路径)。我只希望第一个调用robocopy在继续执行其余代码之前完成执行。因为我希望第二个robocopy跳过所有文件,因为它们已经被复制了。但是,正在发生的情况是,当第一个robocopy复制文件时,脚本的其余部分将运行(即启动第二个robocopy)。下面是代码:

call(["start", "cmd", "/K", "RoboCopy.exe", f"{self.srcEntry.get()}", f"{self.dstEntry.get()}", "*.*", "/E", "/Z", "/MT:8"], stdout=PIPE, shell=True) 
temp2 = Popen(["RoboCopy.exe", f"{self.srcEntry.get()}", f"{self.dstEntry.get()}", "*.*", "/E", "/Z"], stdout=PIPE, stdin=PIPE, shell=True)

编辑1:

复制大文件时,该问题很明显。我正在考虑放置一个睡眠功能,该功能取决于要复制的文件的总大小。但是,由于文件将通过网络传输,因此未考虑上传/下载速度。

python subprocess call popen robocopy
3个回答
0
投票
import os import logging logger = logging.getLogger() def command_runner(command, valid_exit_codes=None, timeout=30, shell=False, decoder='utf-8'): """ command_runner 2019103101 Whenever we can, we need to avoid shell=True in order to preseve better security Runs system command, returns exit code and stdout/stderr output, and logs output on error valid_exit_codes is a list of codes that don't trigger an error """ try: # universal_newlines=True makes netstat command fail under windows # timeout does not work under Python 2.7 with subprocess32 < 3.5 # decoder may be unicode_escape for dos commands or utf-8 for powershell if sys.version_info >= (3, 0): output = subprocess.check_output(command, stderr=subprocess.STDOUT, shell=shell, timeout=timeout, universal_newlines=False) else: output = subprocess.check_output(command, stderr=subprocess.STDOUT, shell=shell, universal_newlines=False) output = output.decode(decoder, errors='backslashreplace') except subprocess.CalledProcessError as exc: exit_code = exc.returncode # noinspection PyBroadException try: output = exc.output try: output = output.decode(decoder, errors='backslashreplace') except Exception as subexc: logger.debug(subexc, exc_info=True) logger.debug('Cannot properly decode error. Text is %s' % output) except Exception: output = "command_runner: Could not obtain output from command." if exit_code in valid_exit_codes if valid_exit_codes is not None else [0]: logger.debug('Command [%s] returned with exit code [%s]. Command output was:' % (command, exit_code)) if output: logger.debug(output) return exc.returncode, output else: logger.error('Command [%s] failed with exit code [%s]. Command output was:' % (command, exc.returncode)) logger.error(output) return exc.returncode, output # OSError if not a valid executable except OSError as exc: logger.error('Command [%s] faild because of OS [%s].' % (command, exc)) return None, exc except subprocess.TimeoutExpired: logger.error('Timeout [%s seconds] expired for command [%s] execution.' % (timeout, command)) return None, 'Timeout of %s seconds expired.' % timeout except Exception as exc: logger.error('Command [%s] failed for unknown reasons [%s].' % (command, exc)) logger.debug('Error:', exc_info=True) return None, exc else: logger.debug('Command [%s] returned with exit code [0]. Command output was:' % command) if output: logger.debug(output) return 0, output # YOUR CODE HERE executable = os.path.join(os.environ['SYSTEMROOT'], 'system32', 'robocopy.exe') mycommand = '"%s" "%s" "%s" "%s"' % (executable, source, dest, options) result, output = command_runner(mycommand, shell=True)

0
投票
WHILE仍在复制中。因此,它会使用第二个robocopy,但是第二个robocopy不会检测到最后一个文件,因此尝试将文件复制过来,但是发现它无法访问该文件,因为该文件已经被使用(由第一个robocopy),因此它等待30秒后再试一次。 30秒后,便可以访问文件并进行复制。我现在想做的是使我的最后一个文件为空的虚拟文件,我不关心它是否被复制两次,因为它是空的。 Robocopy似乎是按照ASCII顺序复制文件的。因此,我已将文件命名为~~~~~ .txt:D

0
投票
while temp2.poll() is not None: # ... do something else, sleep, etc out, err = temp2.communicate()
© www.soinside.com 2019 - 2024. All rights reserved.