Zip a directory using python




















Find centralized, trusted content and collaborate around the technologies you use most. Connect and share knowledge within a single location that is structured and easy to search. The easiest way is to use shutil. It supports both zip and tar formats. If you need to do something more complicated than zipping the whole directory such as skipping certain files , then you'll need to dig into the zipfile module as others have suggested.

As others have pointed out, you should use zipfile. The documentation tells you what functions are available, but doesn't really explain how you can use them to zip an entire directory. I think it's easiest to explain with some example code:.

To add the contents of mydirectory to a new zip file, including all files and subdirectories:. Where zipname is the name of the destination file you want add. Note that you can't run subpackages as the entry point from a zipped archive.

If you have python3. This function will recursively zip up a directory tree, compressing the files, and recording the correct relative filenames in the archive. The archive entries are the same as those generated by zip -r output. Use shutil, which is part of python standard library set.

Using shutil is so simple see code below :. Modern Python 3. As far as I can tell, this is equivalent to George V. Reilly's answer: zips with compression, the topmost element is a directory, keeps empty dirs, uses relative paths. For adding compression to the resulting zip file, check out this link.

With python 3. I've made some changes to code given by Mark Byers. Below function will also adds empty directories if you have them. Examples should make it more clear what is the path added to the zip.

Above is a simple function that should work for simple cases. I have another code example that may help, using python3, pathlib and zipfile.

It should work in any OS. If you want, you could change this to use pathlib for file globbing. If you want a functionality like the compress folder of any common graphical file manager you can use the following code, it uses the zipfile module. Using this code you will have the zip file with the path as its root folder. So many answers here, and I hope I might contribute with my own version, which is based on the original answer by the way , but with a more graphical perspective, also using context for each zipfile setup and sorting os.

Having these folders and them files among other folders , I wanted to create a. Basically, for each iteration over os. So let's say you want to bundle 3 different directory trees under the same archive. The obvious way to go would be to go with shutil, Like the second top answer says so, But if you still wish to go with ZipFile for some reason, And if you are getting some trouble doing that Like ERR 13 in Windows etc , You can use this fix:.

Here's a modern approach, using pathlib, and a context manager. Python Operators. How to Create Iterator in Python. Generators vs Iterators in Python. Is it possible to put an if else statement inside an if else statement? Nested for Loops in Python. How to use a double while loop in python. Python break statement. Python continue Statement.

Is "pass" same as "return None" in Python? Python switch Statement. Creating a Function. Recursion using yield in Python.

Using lambda Function with reduce. Python from Python Package. Type Conversion between numbers. Pythonic way to create a long multi-line string. Negative indexing in List. Delete Tuple Elements in Python.

How to Create a Set. Search in a list of dictionaries. Convert datetime to weekday in Python. How to parse date string and change date format. Get current date and time in Python. How to make a time delay in Python. How to convert a datetime object to milliseconds since unix epoch time in Python. How to get time of whole program execution in Python. How to create a range of dates between two specific dates in Python. How to get the last day of month in Python.

How to add a day to a date in Python. How to Open a File in Python. How to read a file in Python. How to write a list to a file with one item per line. How to move a file in Python. How to delete a specific line in a file. How to list all files of a directory. How to create a dictionary from a csv file in Python.

How to Zip a file with compression in Python. Skip to content. Change Language. Related Articles. Table of Contents. Improve Article. Save Article. Like Article. Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics. This makes the task faster. You can use python scripts for compressing and extracting any large file or directory.

How you can use python3 to compress any file or directory is shown in this tutorial by using various examples. ZipFile 'temp.



0コメント

  • 1000 / 1000