I have a zip file and a directory. The files contained within the zip file shall be copied to the directory if they do not exist within the directory or if they differ (not binary equal). So there are the following two cases.
In the first case I am simply extracting the file directly to the directory (without preserving the directory structure of the zip on purpose).
In the second case I extract the file from the zip to a temp directory and compare them with the following code
extracted_member = os.path.join(TMP_DIR, os.path.basename(zip_member))
with zipfile.open(zip_member) as member_file, open(extracted_member, 'wb') as target_file:
shutil.copyfileobj(member_file, target_file)
print(filecmp.cmp(extracted_member, file_from_dir, False))
So, if I run the program twice without doing anything between the two executions I run into case 2 (as expected). The file comparison should return true at this point (at least to my understanding) but for some reason the result of print(...)
always gives me False
.
Does anybody know what I am doing wrong here or do I have a false understanding of the situation?
The problem is that the output file is probably not closed (so may be incompletely flushed/written) at this point since you're performing the filecmp
operation within the context block.
Do it outside so the file is properly closed:
with zipfile.open(zip_member) as member_file, open(extracted_member, 'wb') as target_file:
shutil.copyfileobj(member_file, target_file)
print(filecmp.cmp(extracted_member, file_from_dir, False))