|  | 
|  | 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