Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compress with 7zip instead of zip, code changing

Tags:

python

I have a code that compress every file in a specific folder with zip but I want to compress it with 7zip, so how to do ?

This is what I have so far:

for date in dict_date:#zipping folders and get same name like the folder
    with ZipFile(os.path.join(src, '{0}.7z'.format(date)), 'w') as myzip:
        for subFolder in dict_date[date]:
            for fil in os.listdir(os.path.join(src, date, subFolder)):
                if not fil.endswith('.7z'):
                    myzip.write(os.path.join(src, date, subFolder, fil))
like image 509
zeromancer Avatar asked Jul 13 '12 08:07

zeromancer


People also ask

Should I compress to 7z or zip?

Archive format — lets you choose the file's format. For the best compression rate, choose 7z. Compression level — the compression time increases with the compression level.

How do I compress files using 7-Zip?

Open the folder containing the files you want to zip, right-click on any free space inside the folder, choose New and then Compressed (zipped) Folder. Name the archive as you wish. Double-click on the newly created archive: a new window will open. Drag and drop any file you want to archive into this folder.

How do I make 7-Zip my default?

Press Win+I to open Settings, and click on Apps. On the left panel, click on Default apps. Then, scroll down on the right panel and click on Set defaults by file type. Here, you can individually select the file types that you want to open with 7zip File Manager by default.

Why can't I compress a zip file?

Again, if you create Zip files and see files that cannot be significantly compressed, it is probably because they already contain compressed data or they are encrypted. If you would like to share a file or some files that do not compress well, you might: Email photos by zipping and resizing them.


1 Answers

You can try the command line method

import subprocess
subprocess.call(['7z', 'a', filename+'.7z', filename])

or for all files in folder

subprocess.call(['7z', 'a', filename+'.7z', "*.*"])
like image 69
Rakesh Avatar answered Sep 17 '22 11:09

Rakesh