1

I have a python script that compresses specific files into a zip file. However I have noticed that a file ".DS_Store" is produced within this zip file. Is there a way I can remove this from the zip file or avoid it being created in the first place in my python script. From what I have found online I think on a windows machine this hidden file appears as "macosx" file.

I've tested the zip file with and without the ".DS_Store" hidden file (I manually deleted it). When I remove it, the zip file is able to be processed correctly and when I leave it in, errors are thrown.

This is how I create the zip file in my python script:

    #Create zip file of all necessary files
    zipf = zipfile.ZipFile(new_path+zip_file_name, 'w', zipfile.ZIP_DEFLATED)
    create_zip(new_path,zipf)
    zipf.close()

Any advice how to approach removing this hidden file would be appreciated.

malbarbo
  • 10,717
  • 1
  • 42
  • 57
Catherine
  • 727
  • 2
  • 11
  • 30

1 Answers1

3

Your code uses a function, create_zip, but you haven't shared the code of that function. Presumably, it loops through the contents of a directory and calls the .write method of the ZipFile instance in order to write each file into the archive. If this is the case, just add some logic to that function to exclude any files called .DS_Store.

def create_zip(path, zipfile):
    files = os.listdir(path)
    for file in files:
        if file != '.DS_Store':
            zipfile.write(file)
James Scholes
  • 7,686
  • 3
  • 19
  • 20