Search code examples
pythonzippython-zipfile

How to change the output folder using ZipFile?


I need to zip each individual file based on the list in excel from one folder (Location1) to another folder (Location2) but currently I'm facing a problem whereby the output for my zip file is been placed at the root folder.

Here is my code.

DirectorySource = 'D:\Location1'

DirectoryDestination = 'D:\Location2'

data = openpyxl.load_workbook(r'D:\File_List.xlsx')
df = data['Sheet1']
        
for i in range(2, df.max_row + 1):
           cell_obj = df.cell(row=i, column = 1)
           ZipFile(cell_obj.value+'.zip',mode='w').write(DirectorySource+'/'+cell_obj.value+'.txt',
           basename(DirectorySource+'/'+cell_obj.value+'.txt'),compress_type=ZIP_DEFLATED)

How can I solve this issue?


Solution

  • To put .zip file in different folder you have to use path in ZipFile, not in write

    destination  = os.path.join(DirectoryDestination, cell_obj.value+'.zip')
    
    ZipFile(destination, ...)