Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit de57dc8

Browse files
Add searchSize
1 parent 49ecbf5 commit de57dc8

File tree

2 files changed

+79
-1
lines changed

2 files changed

+79
-1
lines changed

‎README.md‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ pip install -r requirements.txt
4141

4242
- Contents
4343
- [Code Destroyer](#Code-Destroyer)
44-
- [Phone Number and Email Extractor](#Phone_Number-and-Email-Extractor)
44+
- [Phone Number and Email Extractor](#Phone-Number-and-Email-Extractor)
45+
- [Search files based on Size](#Search-files-based-on-Size)
4546

4647
- [License](LICENSE)
4748

@@ -73,6 +74,18 @@ python3 phoneAndEmail.py
7374

7475
Two files, `emails.txt` and `phoneNumbers.txt` would be created in the same directory containing the emails and phone numbers from the copied text.
7576

77+
### Search files based on Size
78+
79+
Lets you search through a folder based on file size. Asks user for folder path and size. Files and subfolders inside the folder, greater than or equal to the input size would be displayed.
80+
81+
#### Usage:
82+
83+
```py3
84+
python3 searchSize.py
85+
```
86+
87+
When prompted, enter the minimum size (in bytes) and the folder where files are to be searched. If the path entered is correct, the files, above and equal the size entered would be displayed.
88+
7689
---
7790

7891
File Templates taken from [awesome-bashrc](https://github.com/aashutoshrathi/awesome-bashrc) and [HackerRank-Test-Case-Generator](https://github.com/aashutoshrathi/HackerRank-Test-Case-Generator/).

‎searchSize/searchSize.py‎

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
"""
2+
searchSize.py - Lets you search through a folder based on file size.
3+
Files and subfolders greater than or equal to the size entered, would
4+
be displayed.
5+
6+
Usage: run "python3 searchSize.py". When prompted, enter the minimum
7+
size (in bytes) and the folder where files are to be searched.
8+
If the path entered is correct, the files, above and equal the size
9+
entered would be displayed.
10+
"""
11+
12+
import os
13+
import sys
14+
15+
# Dictionary to save large files/folders with path as key ad size as
16+
# value
17+
large = {}
18+
19+
# Get size of folder and save path of files and folders greater than
20+
# input size to the dict. large
21+
def getSize(folPath):
22+
totalSize = 0
23+
for folderName, subfolders, fileNames in os.walk(folPath):
24+
for subfolder in subfolders:
25+
subfolderPath = os.path.join(folderName, subfolder)
26+
subfolderSize = getSize(subfolderPath)
27+
if subfolderSize >= size:
28+
large[subfolderPath] = subfolderSize
29+
totalSize += subfolderSize
30+
31+
for fileName in fileNames:
32+
filePath = os.path.join(folderName, fileName)
33+
if not os.path.islink(filePath): # Skip if symbolic link
34+
fileSize = os.path.getsize(filePath)
35+
if fileSize >= size:
36+
large[filePath] = fileSize
37+
totalSize += fileSize
38+
return totalSize
39+
40+
# Input minimum size
41+
size = int(input("Enter the minimum size (in bytes)\n"))
42+
43+
# Input folder name
44+
folder = input("Enter the path of the folder to search\n")
45+
folder = os.path.abspath(folder) # Absolute path
46+
47+
# Verify if folder name and path exists
48+
if not os.path.exists(folder):
49+
print("The folder path entered does not exists.")
50+
sys.exit()
51+
else:
52+
folderSize = getSize(folder)
53+
54+
# If no files/folders found
55+
if large == {}:
56+
print(f"There are no files or folders with size greater than {size} bytes.")
57+
58+
# Print paths with size
59+
else:
60+
print(f"The files and folders with size greater than {size} are:")
61+
for path in large.keys():
62+
if os.path.isfile(path):
63+
print(f"The size of the file {path} is: {large[path]} bytes.")
64+
elif os.path.isdir(path):
65+
print(f"The size of the folder {path} is: {large[path]} bytes.")

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /