Python Forum
Making Zip file of a file and Directory
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Making Zip file of a file and Directory
#1
Photo 
Hi

I am new to this forum and new user of python.

I have list of files and directories (contain files and subdirectories) in CSV file. The file name and main directory name is same in each CSV row . I want to make the zip file of file and directory in each row having zip file name as file name.

csv file:
Output:
File Directory C:\\Users\\XYZ\\Desktop\\TEST\\\TEST.abc C:\\Users\\XYZ\\Desktop\\TEST\\\TEST.XYZ C:\\Users\\XYZ\\Desktop\\TEST\\\TEST1.abc C:\\Users\\XYZ\\Desktop\\TEST\\\TEST1.XYZ C:\\Users\\XYZ\\Desktop\\TEST\\\TEST2.abc C:\\Users\\XYZ\\Desktop\\TEST\\\TEST2.XYZ C:\\Users\\XYZ\\Desktop\\TEST\\\TEST3.abc C:\\Users\\XYZ\\Desktop\\TEST\\\TEST3.XYZ
Python code:

path = "C:\\Users\\XYZ\\Desktop\\TEST.csv"    ## CSV file which contain listing of files and directory path
df = pd.read_csv(path) 

for index, row in df.iterrows():
    target1 = row.File
    target2 =row.Directory

with ZipFile("TEST.zip", "w") as newzip:
          newzip.write(target1)
          newzip.write(target2)
It gives only the zip file of last file and directory which is TEST3

Question:

I want to make zip file of each row and corresponding directory as one zip file having name as file name in 1st column
Could you please anyone suggest, how can i process further.

Many Thanks
Nasir
Larz60+ write Oct-07-2022, 09:22 AM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Fixed for you this time. Please use BBCode tags on future posts.
Note: Output tags also work well for input data
Reply
#2
Looks like you're opening a new zipfile (overwriting any previous one) inside your for loop.

I think you want to open the zipfile outside the for loop and only do the write() inside.

But why is column2 titled "directory"? Those entries don't look like directories to me.
Reply
#3
That is just a sample CSV file, however these are actually a directory names
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  file open "file not found error" shanoger 8 1,087 Dec-14-2023, 08:03 AM
Last Post: shanoger
  Need to replace a string with a file (HTML file) tester_V 1 761 Aug-30-2023, 03:42 AM
Last Post: Larz60+
  How can I change the uuid name of a file to his original file? MaddoxMB 2 921 Jul-17-2023, 10:15 PM
Last Post: Pedroski55
  Using pyinstaller with .ui GUI files - No such file or directory error diver999 3 3,305 Jun-27-2023, 01:17 PM
Last Post: diver999
  Reading data from excel file –> process it >>then write to another excel output file Jennifer_Jone 0 1,091 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone
  Extract file only (without a directory it is in) from ZIPIP tester_V 1 981 Jan-23-2023, 04:56 AM
Last Post: deanhystad
Thumbs Up Need to compare the Excel file name with a directory text file. veeran1991 1 1,111 Dec-15-2022, 04:32 PM
Last Post: Larz60+
  no such file or directory in SFTP saisankalpj 2 1,540 Nov-25-2022, 11:07 AM
Last Post: DeaD_EyE
  Failed to execute child process (No such file or directory) uriel 1 1,651 Sep-15-2022, 03:48 PM
Last Post: Gribouillis
  Need Help: Convert .pcl file to .pdf file ManuRaval 6 2,541 Sep-13-2022, 01:31 PM
Last Post: ManuRaval

Forum Jump:

User Panel Messages

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