'How to create a zipfile and write it to multiple directories/folders?

I have a single .txt file that I want to compress into a zipfile and write into multiple directories.

This function creates a list of directories that I want to read the zipped .txt file:

dir_path = 'N:/Project/50813_Medicaid_UPL_DSH/NJ1/Data Files from CMS/UPL/Guidance_Document_Extract/_ticket5250_sandbox/2021'
directories_list = retrieve_file_paths(dir_path)
directories_list

This is the output:

N:/Project/50813_Medicaid_UPL_DSH/NJ1/Data Files from CMS/UPL/Guidance_Document_Extract/_ticket5250_sandbox/2021\Alabama\AL_ICF_2021
N:/Project/50813_Medicaid_UPL_DSH/NJ1/Data Files from CMS/UPL/Guidance_Document_Extract/_ticket5250_sandbox/2021\Alabama\AL_IP_2021
N:/Project/50813_Medicaid_UPL_DSH/NJ1/Data Files from CMS/UPL/Guidance_Document_Extract/_ticket5250_sandbox/2021\Alabama\AL_OP_2021
N:/Project/50813_Medicaid_UPL_DSH/NJ1/Data Files from CMS/UPL/Guidance_Document_Extract/_ticket5250_sandbox/2021\Alabama\AL_PHYS_2021
N:/Project/50813_Medicaid_UPL_DSH/NJ1/Data Files from CMS/UPL/Guidance_Document_Extract/_ticket5250_sandbox/2021\Alabama\AL_NF_2021
N:/Project/50813_Medicaid_UPL_DSH/NJ1/Data Files from CMS/UPL/Guidance_Document_Extract/_ticket5250_sandbox/2021\Alabama\AL_CLNC_2021
N:/Project/50813_Medicaid_UPL_DSH/NJ1/Data Files from CMS/UPL/Guidance_Document_Extract/_ticket5250_sandbox/2021\Alabama\AL_IMD_2021
N:/Project/50813_Medicaid_UPL_DSH/NJ1/Data Files from CMS/UPL/Guidance_Document_Extract/_ticket5250_sandbox/2021\Alabama\AL_PRTF_2021

NOTE: I want the zipfile placed into the last folder (ex. AL_ICF_2021)

This is the code with the path to the .txt file and my attempt to compress it and write it to the directories list.

# Assign the name of the directory to zip
zipfile_name = 'N:/Project/50813_Medicaid_UPL_DSH/NJ1/Data Files from CMS/UPL/Guidance_Document_Extract/_ticket5250_sandbox/guidancedocnotes.txt'
          
# Compress .txt into a zipfile
zip_file = zipfile.ZipFile('guidancedocnotes.zip', 'w')
zip_file.write(zipfile_name, compress_type=zipfile.ZIP_DEFLATED)
    
for folder in folder_list:
    complete_name = os.path.join(zip_file, folder_list)
    print(complete_name)

This is the error message I'm getting:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-37-57432b3f2938> in <module>
     24 # Call the main function
     25 if __name__ == "__main__":
---> 26     main()

<ipython-input-37-57432b3f2938> in main()
     18 
     19     for folder in folder_list:
---> 20         complete_name = os.path.join(zip_file, folder_list)
     21         print(complete_name)
     22 

~\AppData\Local\Continuum\Anaconda3\lib\ntpath.py in join(path, *paths)
     74 # Join two (or more) paths.
     75 def join(path, *paths):
---> 76     path = os.fspath(path)
     77     if isinstance(path, bytes):
     78         sep = b'\\'

TypeError: expected str, bytes or os.PathLike object, not ZipFile

I basically want to place a separate zipfile called guidancedocsnote.zip into each one of the directories listed above. What can I do to get this to work?



Solution 1:[1]

I am not sure what your actual issue is. Looks like you are 90% of the way. I am not familiar with 'os.path.join' but assuming it is producing a fully qualified path, then you just need to copy and paste the zip file into that location instead of printing the path name.

Might need to provide a bit more information here.

Solution 2:[2]

os.path.join takes in the src and target directory and returns a path. At the moment you are passing in the zipfile object instead of a string. os.path.join will not save anything, instead it will return a path to which you could save something.

Your question is not entirely clear, but I think this it what you want

# Compress .txt into a zipfile
zip_file = zipfile.ZipFile('guidancedocnotes.zip', 'w')

for folder in folder_list:
   save_path = os.path.join(folder, zipfile_name)
   zip_file.write(save_path, compress_type=zipfile.ZIP_DEFLATED)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 max_settings
Solution 2 Mitchell van Zuylen