Python Forum

Full Version: Making Zip file of a file and Directory
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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
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.
That is just a sample CSV file, however these are actually a directory names