- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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 tar file using Python?
Use the tarfile module to create a zip archive of a directory. Walk the directory tree using os.walk and add all the files in it recursively.
For example
import os import tarfile def tardir(path, tar_name): with tarfile.open(tar_name, "w:gz") as tar_handle: for root, dirs, files in os.walk(path): for file in files: tar_handle.add(os.path.join(root, file)) tardir('./my_folder', 'sample.tar.gz') tar.close()
The above code will compress the contents of my_folder in a file 'sample.tar.gz'. and store it in the current directory.
- Related Articles
- How are files extracted from a tar file using Python?
- How are files added to a zip file using Python?
- How to create a tar file using Python?
- Read and write tar archive files using Python (tarfile)
- How to merge multiple files into a new file using Python?
- How to concatenate two files into a new file using Python?
- How to spilt a binary file into multiple files using Python?
- How to extract all the .txt files from a zip file using Python?
- How to save files using a File Chooser in JavaFX?
- How to open multiple files using a File Chooser in JavaFX?
- How to Download and Extract Tar Files with One Command in Linux
- The best way to compress and extract files using the tar command on linux
- How to convert PDF files to Excel files using Python?
- How to read all files in a folder to a single file using Java?
- Python - Write multiple files data to master file

Advertisements