简体   繁体   中英

Password protected zip file using Python

I'm trying to password protect zip files that I'm creating in a python script but have not been able to figure out why when I try to unzip it does not accept the password that the script created.

#!/usr/bin/env python2.6
import subprocess, random, os

file = "test.csv"
s = "abcdefghijklmnopqrstuvwxyz01234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()?"
zipPass = "".join(random.sample(s,12))
print zipPass
devnull = open(os.devnull, 'w')
rc = subprocess.call("zip -P "+str(zipPass)+" "+str(file)+".zip "+str(file), shell=True, stdout=devnull, stderr=devnull)

When I run the script and try to unzip I get the following;

user@server:/tmp/zip> zip.py
xxxxxxxxx
user@server:/tmp/zip> unzip test.csv.zip
Archive:  test.csv.zip
[test.csv.zip] test.csv password:
password incorrect--reenter:

I've tried copying/pasting the password and also manually entering just to be sure.

Any ideas?

Tested your script worked for me. just to be sure , remove created zip file run script and check if it is zip file which created by your script. I said this because you didn't define path to store zip file, so it is possible your file is created somewhere else and you are looking to wrong one.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM