Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 914
Martin Melka Avatar asked Nov 24 '16 17:11

Martin Melka


People also ask

What is the Shutil function in Python?

copy() method in Python is used to copy the content of the source file to the destination file or directory. It also preserves the file's permission mode but other metadata of the file like the file's creation and modification times is not preserved.

What is the use of Rmtree ()?

rmtree() is used to delete an entire directory tree, path must point to a directory (but not a symbolic link to a directory). Parameters: path: A path-like object representing a file path.

What is the use of shutil copy in Python?

shutil.copy () method in Python is used to copy the content of the source file to the destination file or directory. It also preserves the file’s permission mode but other metadata of the file like the file’s creation and modification times is not preserved.

How do I archive files in shutil?

The shutil module has two functions — make_archive () and unpack_archive () — that can exactly be the solution. The second argument to make_archive () is the desired output format. To get a list of supported archive formats, use get_archive_formats ().

How to copy and remove files and directories in Python?

It comes under Python’s standard utility modules. This module helps in automating the process of copying and removal of files and directories. In this article, we will learn this module. shutil.copy () method in Python is used to copy the content of the source file to the destination file or directory.

How to create a directory named path in Python?

There are different methods available in the OS module for creating a director. These are – os.mkdir () method in Python is used to create a directory named path with the specified numeric mode. This method raise FileExistsError if the directory to be created already exists. path: A path-like object representing a file system path.


1 Answers

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.

like image 147
lucasg Avatar answered Sep 22 '22 19:09

lucasg