Reputation: 1160
BACKGROUND
I have some directory in my home directory called delivery_content
. Inside that directory there are two folders content_123
and content_456
. Each directory contains a text file. So the over all structure looks like the following.
-delivery_content
-content_123/
-content_123.txt
-content_456/
-content_456.txt
I am writing code that when given a directory it will zip all of it's contents.
CODE
import os
import shutil
from pathlib import Path
from datetime import datetime
DOWNLOAD_DIR = Path(os.getenv("HOME")) / "delivery_content"
date = datetime.today().strftime("%Y-%m-%d")
delivery_name = f"foo_{date}"
shutil.make_archive(str(DOWNLOAD_DIR / delivery_name), "zip", str(DOWNLOAD_DIR))
print("Zipping Has Complete")
So for the given code above i expect to see a foo_todays_date.zip in the DOWNLOAD_DIR
and when i unzip it i expect to see the following
-content_123/
-content_123.txt
-content_456/
-content_456.txt
Is there a way i can avoid having a .zip inside my zipfile?
Upvotes: 0
Views: 63
Reputation: 1618
The problem is that you are putting the zip file inside the directory that you are archiving. make_archive
is probably creating an empty file to verify it can write to the destination before it starts archiving. So it then sees the empty zip file as a file to archive. The only way I see around this using the make_archive
function is to put the archive file outside of the directory to archive like below:
shutil.make_archive(str(DOWNLOAD_DIR / ".."/ delivery_name), "zip", str(DOWNLOAD_DIR))
You can always move the zip file into the DOWNLOAD_DIR afterward.
Upvotes: 1