Pythonからファイルを圧縮するためにlzmaを使用しています。圧縮後、ファイルを別の場所に移動したいと考えています。問題は、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