Python Forum
Created zipfile without all the subfolder?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Created zipfile without all the subfolder?
#1
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 ,
Reply
#2
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)
Reply
#3
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))
Reply
#4
this is what I wanted

thank you !!

so simple solution for a simple problem
:-)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Compare folder A and subfolder B and display files that are in folder A but not in su Melcu54 3 466 Jan-05-2024, 05:16 PM
Last Post: Pedroski55
  zipfile module error pseudo 3 694 Jun-30-2023, 03:52 PM
Last Post: Gribouillis
  [Solved by deanhystad] Create a zip file using zipfile library DZ_Galaxy 2 1,106 Aug-17-2022, 04:57 PM
Last Post: DZ_Galaxy
  Can ZipFile be used to extract hidden files? AiedailEclipsed 0 1,586 Mar-22-2022, 05:21 PM
Last Post: AiedailEclipsed
  how to extract tiff images from the subfolder into. hocr format in another similar su JOE 0 1,131 Feb-16-2022, 06:28 PM
Last Post: JOE
Photo Moving 2 photos from each subfolder to another folder Blacklonewolf 1 2,005 Oct-28-2021, 04:07 PM
Last Post: DeaD_EyE
  Create ZIP file not working using ZipFile? korenron 1 2,062 Jun-13-2021, 04:15 PM
Last Post: korenron
  zipfile sagpal 2 2,175 Jun-03-2020, 08:35 PM
Last Post: sagpal
  zip file variable prints <zipfile.ZipFile object at 0x7f83fd13bd90> ? Rsh 9 4,006 Jun-27-2019, 02:00 PM
Last Post: Rsh
  get all .exe files from folder and subfolder and then copy them to another location evilcode1 1 4,166 Jul-20-2018, 03:39 PM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020