0

New to Python, but old to programming. I have come here because no matter when I search for a Python reference, my best answers have been here. And when it comes to the current question, believe me I have looked here, extensively, and while I get the answers I want, they do not always work.

So Here's my question. Very simple, I just want to use Python to zip files up, and verify said zip file.

Here is the relevant code :

import zipfile

archive_file_list = ['archive1','archive2']

LogIt(log_file,"checking for existance of archive directory")
if os.path.isdir(archive_path) == True:
    LogIt(log_file,'archive directory found')
    LogIt(log_file,'looking for files to archive')
    for a in archive_file_list:
        target_file = archive_path + a + '.txt'
        LogIt(log_file,'looking for file : ' + target_file)
        if os.path.isfile(target_file) == True:
            LogIt(log_file,'file found')
            zip_file = archive_path + a + '.zip'
            LogIt(log_file,'creating zip file : ' + zip_file)
            zf = zipfile.ZipFile(zip_file,'w')
            zf.write(target_file,zip_file,compress_type=zipfile.ZIP_DEFLATED)
            zf.close
            LogIt(log_file,'Zip file created')
            LogIt(log_file,'verifying')
            if os.path.isfile(zip_file) == True:
                LogIt(log_file,'zip file exists')
                LogIt(log_file,'verifying zip file')
                if zipfile.is_zipfile(zip_file) == True:
                    LogIt(log_file,'file ' + zip_file + ' verified')
                else:
                    LogIt(log_file,'file ' + zip_file + ' not verified')
                # print_info(zip_file)
            else:
                LogIt(log_file,'unable to verify zip file')
        else:
            LogIt(log_file,'file not found')
else:
    LogIt(log_file,'archive path not found, no files to zip')

For the most part it works fine, the designated file is zipped. I can read it via pkzip or Rar on any of the machines I move it to. But when I actually check to see if this is a valid zip file, I get shut down, looking at the file with a decent editor shows that it does begin with "PK", or "Pk", I forget, but it certainly appears to be valid.

if zipfile.is_zipfile(zip_file) == True:

this always returns False, I just don't get it.

Thanks in advance for your assistance. jim

1
  • sorry, that cut/paste of the code was ugly as heck, trust me when I say it was syntactically correct. Commented Dec 22, 2015 at 6:55

1 Answer 1

1

This line is your problem:

zf.close

You are not actually calling close because you omitted the parens. So you just have a statement with no effect whatsoever. The file gets closed after your python program exits so you have a valid zipfile at that point, but not when you do your check. Change this to zf.close() and I bet you see your zipfile verify.

Sign up to request clarification or add additional context in comments.

1 Comment

And verified! I thought it was something like that. Thank you for the help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.