My Code
import shutil
import os
from pathlib import Path
from datetime import datetime
DOWNLOAD_DIR = Path("/Users/my_name/Desktop/Zip Test/g_2020-12-10_3/")
ZIPPED_DIR = Path("/Users/my_name/Desktop/Zip Test/g_2020-12-10_3.zip")
ZIPPED_DIR.mkdir(exist_ok=True)
def make_archive(source, destination):
delivery_file_name = destination.split("/")[-1]
delivery_name = delivery_file_name.split(".")[0]
format = delivery_file_name.split(".")[1]
archive_from = os.path.dirname(source)
archive_to = os.path.basename(source.strip(os.sep))
shutil.make_archive(delivery_name, format, archive_from, archive_to)
shutil.move(
"%s.%s" % (delivery_name, format),
"/Users/my_name/Desktop/Zip Test/zipped_content",
)
make_archive(str(DOWNLOAD_DIR), str(ZIPPED_DIR))
Description
I am very new to python so i had a very hard time getting shutil.make_archive to do what i want. This code is finally working and i feel it is a little hard to read so i was wondering if someone can help me refactor this to simplify it. I don't want to loose the functionality of what i am doing. In my code i basically go to my /Users/my_name/Desktop/Zip Test
and inside it is a folder called g_2020-12-10_3
which contains the contents i want to zip up so i do that.
When i unzip the contents it unzips folder named g_2020-12-10_3
and inside it are the contents.
I want to keep this functionality but at the same time simplify the code below and any help would be appreciated here.