- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How are files added to a zip file using Python?
Use the zipfile module to create a zip archive of a directory. Walk the directory tree using os.walk and add all the files in it recursively.
import os import zipfile def zipdir(path, ziph): # ziph is zipfile handle for root, dirs, files in os.walk(path): for file in files: ziph.write(os.path.join(root, file)) zipf = zipfile.ZipFile('Zipped_file.zip', 'w', zipfile.ZIP_DEFLATED) zipdir('./my_folder', zipf) zipf.close()
The above code will zip the contents of my_folder in a file 'Zipped_file.zip'. and store it in the current directory.
- How are files added to a tar file using Python?
- How to extract all the .txt files from a zip file using Python?
- How to create a zip file using Python?
- How are files extracted from a tar file using Python?
- How to Zip / Unzip files or folders using PowerShell?
- Working with zip files in Python
- How to zip a folder recursively using Python?
- How to spilt a binary file into multiple files using Python?
- How to merge multiple files into a new file using Python?
- How to concatenate two files into a new file using Python?
- How to save files using a File Chooser in JavaFX?
- How to create a zip archive of a directory using Python?
- How to open multiple files using a File Chooser in JavaFX?
- How to read a single file inside a zip archive with PHP
- How to convert PDF files to Excel files using Python?