Skip to content

Commit 4ef20c7

Browse files
committed
Script to convert folders to Zip file avinashkranjan#14
1 parent 498d56d commit 4ef20c7

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

Folder-to-Zip/ReadME.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Folder - to - Zip
2+
3+
## Utility
4+
5+
* To convert folders into ZIP format
6+
7+
## How to use
8+
9+
* ### Step 1
10+
11+
> Put the folder you want to convert in the same directory
12+
13+
* ### Step 2 -> Run the script
14+
15+
Windows:
16+
17+
> py folder-to-zip.py
18+
19+
Linux:
20+
21+
> python3 folder-to-zip.py
22+
23+
## You're all set! The Zip file for the selected folder will be created

Folder-to-Zip/folder-to-zip.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import os
2+
import zipfile
3+
from random import randrange
4+
5+
6+
def zip_dir(path, zip_handler):
7+
for root, dirs, files in os.walk(path):
8+
for file in files:
9+
zip_handler.write(os.path.join(root, file))
10+
11+
12+
if __name__ == '__main__':
13+
to_zip = input("""
14+
Enter the name of the folder you want to zip
15+
(N.B.: The folder name should not contain blank spaces)
16+
>
17+
""")
18+
to_zip = to_zip.strip() + "/"
19+
zip_file_name = f'zip{randrange(0,10000)}.zip'
20+
zip_file = zipfile.ZipFile(zip_file_name, 'w', zipfile.ZIP_DEFLATED)
21+
zip_dir(to_zip, zip_file)
22+
zip_file.close()
23+
print(f'File Saved as {zip_file_name}')

0 commit comments

Comments
 (0)