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:
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:
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')
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.
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.
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.
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 ().
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.
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.
This "bug" has been fixed in October 2016 : https://github.com/python/cpython/commit/666de7772708047b63125126b0147931571254a4
Here the diff :
Apparently, you need to update to either Python 3.5.3 or 3.6.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With