Python Forum
Created zipfile without all the subfolder? - 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: Created zipfile without all the subfolder? (/thread-34069.html)



Created zipfile without all the subfolder? - korenron - Jun-23-2021

Helllo ,
I want to create a zip file that contiens only 1 file
this is what I have done:
 with ZipFile(ZipLocation, 'w') as zipObj:
        zipObj.write(OutputFile, compress_type=compression)
this create me 1 zip file as planed

but when I open the zip file I need to go to 4 folders
/home/pi/logs/Outputfile.txt


I have also try to cut the location

ZipLocation = '/home/pi/logs/CanBusData.zip'
ZipName = ZipLocation.split(os.sep)[-1]
    with ZipFile(ZipLocation, 'w') as zipObj:
        zipObj.write(OutputFile, arcname=ZipName,  compress_type=compression)

but then he create me a zip inside the zip and it't corrupted


what do I need to do in order to make put only the OutputFile.txt in the zip ?

Thanks ,


RE: Created zipfile without all the subfolder? - Axel_Erfurt - Jun-23-2021

this works for me,

import zipfile
zip_location = "test.zip"
text_file = "test.txt"

with zipfile.ZipFile(zip_location, 'w') as zipObj:
       zipObj.write(text_file)



RE: Created zipfile without all the subfolder? - Axel_Erfurt - Jun-23-2021

or if you want to use full paths,

import zipfile       
from os.path import basename

zip_location = "/tmp/test.zip"
text_file = "/tmp/test.txt"

with zipfile.ZipFile(zip_location, 'w') as zipObj:
    zipObj.write(text_file, basename(text_file))



RE: Created zipfile without all the subfolder? - korenron - Jun-23-2021

this is what I wanted

thank you !!

so simple solution for a simple problem
:-)