Martin Melka
Martin Melka

Reputation: 7789

Python's shutil.make_archive() creates dot directory on Windows

Using shutil.make_archive('newarchive', 'zip', <directory>) to create a ZIP archive in Python 3.5 does not behave as expected on Windows.

On Linux it works correctly, all files and folders inside directory are archived and visible in the zip file. However, on Windows an extra folder is created - the dot folder .. See screenshot:

enter image description here

The folder itself is empty, but I want to get rid of it altogether (another process is very strict about the structure). A workaround would be not using make_archive() and manually create ZipFile, but I feel that the function should work in the first place.

Is this a bug or am I missing something?


Edit: dot file is present in 7Zip as well as Total Commander. This is the shortest working snippet for me (Python 3.5.1, Windows 10):

import shutil
import os
os.chdir('C:/Users/melka/Downloads')
shutil.make_archive('testing', 'zip', 'zip_test')

This creates a new ZIP from contents of C:\Users\melka\Downloads\zip_test, which ends up being: enter image description here

However, manually creating the zip using this code does not create the dot file:

import os
import zipfile


def make_zip(zip_name, path):
    zip_handle = zipfile.ZipFile(zip_name, 'w', zipfile.ZIP_DEFLATED)

    os.chdir(path)
    for root, dirs, files in os.walk('.'):
        for file in files:
            zip_handle.write(os.path.join(root, file))

os.chdir('C:/Users/melka/Downloads')
make_zip('anotherzip.zip', 'zip_test')

enter image description here

Upvotes: 11

Views: 3066

Answers (1)

lucasg
lucasg

Reputation: 11002

This "bug" has been fixed in October 2016 : https://github.com/python/cpython/commit/666de7772708047b63125126b0147931571254a4

Here the diff : enter image description here

Apparently, you need to update to either Python 3.5.3 or 3.6.

Upvotes: 3

Related Questions