Python Forum
how can i adding custom path for saving the zip_file - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: how can i adding custom path for saving the zip_file (/thread-35206.html)



how can i adding custom path for saving the zip_file - demonvictor - Oct-09-2021

I am creating a zip file using the zipfile module.It works like a charm. but thatsĀ file, saved in the executed script place.
my script path is a:
c:/User/Administrator/script.py
and the zipfile saved in:
c:/User/Administrator/backup.zip

but i want, creating a zipfile, in another path, like this:
d:/backups/backup.zip

my code like this:
import zipfile

zip_file = zipfile.ZipFile("backup.zip", 'w')
with zip_file:
    for file in filePaths:
    zip_file.write(file)
my question is a how can i adding custom path for saving the zip_file. because i have not an enough space in C:

tnx a lot.


RE: how can i adding custom path for saving the zip_file - snippsat - Oct-09-2021

You give path to where read from(if not in same folder as script) and destination.
Example.
import zipfile
import pathlib

file_path = r'G:\div_code\answer\weather'
dst = r'G:\div_code\answer\backup.zip'
with zipfile.ZipFile(dst, 'w') as zip_file:
    for file in pathlib.Path(file_path).iterdir():
        if file.is_file():
            print(f'zipped --> {file.name}')
            zip_file.write(file)
Output:
zipped --> file_1_0_.txt zipped --> file_33.txt zipped --> key.ini zipped --> open_weather.py zipped --> to_zip.txt