-3

I want to create a file .zip in python that includes an entire folder + another file,how can i make it?

files.zip:
 |-mydir
 |-file.txt

Thanks

Mirko
  • 21
  • 5
  • You tagged zipfile so I assume you are familiar with the python module zipfile: https://docs.python.org/3/library/zipfile.html Have you tried it out? Can you share some code? – Tin Nguyen Feb 26 '20 at 14:03
  • Does this answer your question? [Create .zip in Python?](https://stackoverflow.com/questions/14568647/create-zip-in-python) – bartonstanley Feb 26 '20 at 15:08

1 Answers1

0

Solved. In according with zip documentation

with ZipFile('Your_zip_file.zip','w') as zip:
    # writing each file one by one
    for file in os.listdir(str(Path("folder_to_zip/"))):
        zip.write('folder_to_zip/'+str(file))
    zip.close()

Use:

  • 'r' to read an existing file,
  • 'w' to truncate and write a new file,
  • 'a' to append to an existing file, or
  • 'x' to exclusively create and write a new file.

Hope this will be helpful for others.

Mirko
  • 21
  • 5