You can zip files with python.
There is also `zipfile``` in the standard python library, but I can't put a password on zip. I wanted to set a password this time, so I tried using
`pyminizip```.
It is windows10 (Pro). python is `` `Python 3.7.1```. It doesn't really matter. This article is for Windows users like me who continue to smoke the sweet honey of Microsoft. UNIX people do something on their own!
Just type the following command
pip install pyminizip
It's okay to write like this
test_zip.py
import pyminizip
pyminzip.compress(
'Source file name',
'Source file prefix',
'Output file name.zip',
'password',
int(0) #Compression rate: 0-9(0 is uncompressed)
)
1st argument: File name to be zipped 2nd argument: Directory where you want to put the file after zip compression Third argument: output file name Fourth argument: password Fifth argument: compression rate. You can specify 0-9. 0 is uncompressed
For example, if you execute pyminizip.compress ('src / src.txt','dir','output.zip','password', int (0))` ``, it will be in the place where it was executed. A zip file with `` dir / src.txt
inside is output with the file name output.zip
.
If you are Japanese, there are times when you want to output in Japanese, such as the source file name or output file name, right? However, `` `pyminizip``` will give an error if you just throw a string normally. The error comes out as follows.
Traceback (most recent call last):
File "test_zip.py", line 28, in <module>
test1()
File "test_zip.py", line 16, in test1
int(0)
OSError: error in opening output/test.txt for reading
So, what to do is as follows.
test_zip.py
import pyminizip
pyminzip.compress(
'Source file name'.encode('cp932'),
'Source file prefix'.encode('cp932'),
'Output file name.zip'.encode('cp932'),
'password'.encode('cp932'),
int(0) #Compression rate: 0-9(0 is uncompressed)
)
If you change the character code to cp932
, it will pass.
Probably because Windows uses `` `cp932```.
I want to create a zip file with a password, but I can't do it with the standard library `zipfile```, so I tried to use
pyminizip``` did.
I hope it helps someone.
It is also good to look back.
pyminizip: Official guy
Recommended Posts