我正在使用lzma从python压缩文件。压缩后我想将文件移动到另一个位置。问题是,当我使用lzma的参数调用subprocess.Popen时,它立即返回而不等待压缩完成,我最终移动了部分文件。
我的代码:
def CheckOutputLzma(args, outfile, cwd=None, print_stdout=False, print_stderr=True,
fail_if_stderr=False):
if not cwd:
cwd = os.getcwd()
child = subprocess.Popen(args, stdout=outfile, stderr=subprocess.PIPE, cwd=cwd)
stdout, stderr = child.communicate()
if child.returncode or (stderr and fail_if_stderr):
raise CalledProcessError(cwd, args, stdout + stderr)
if print_stdout:
sys.stdout.write(stdout)
if print_stderr:
sys.stderr.write(stderr)
return stdout
def CompressLibrary(inputFile , library , outputPath):
if os.path.isfile(outputPath+"/"+library+ ".lzma"):
os.remove(outputPath+"/"+library+ ".lzma")
command = (['lzma'] + ['-5'] + ['-c'] + [inputFile])
with open(outputPath+"/"+library+ ".lzma","wb") as out:
CheckOutputLzma(command, out)
#Wait until the lzma is complete
check_command = (['lzma'] + ['-t'] + [outputPath+"/"+library+ ".lzma"])
while True:
#stdout = build_utils.CheckOutput(check_command, None, True, True)
stdout = subprocess.check_output(check_command)
print("STDOUT : " + stdout)
break
if (not stdout):
break