|
| 1 | +import os |
| 2 | +path = input("Enter a path (Copy from above) : ") |
| 3 | +extension = input("Enter an extension(Eg: .py, .jpg ) : ") |
| 4 | +flag = input("Do you want to print the whole directory summary (y/n) : ") |
| 5 | +pre_files = list() |
| 6 | +final_files = list() |
| 7 | + |
| 8 | +def validate_extension(): |
| 9 | + count = 0 |
| 10 | + for i in extension: |
| 11 | + if i is ".": |
| 12 | + count += 1 |
| 13 | + if count!=1: |
| 14 | + raise Exception("Invalid extension. Check the examples given") |
| 15 | + |
| 16 | +def preprocess(): |
| 17 | + print("Please wait while proccessing...") |
| 18 | + global final_files |
| 19 | + global pre_files |
| 20 | + for (dirpath, dirnames, filenames) in os.walk(path): |
| 21 | + pre_files.append(filenames) |
| 22 | + |
| 23 | + for i in pre_files: |
| 24 | + if str(type(i))=="<class 'list'>": |
| 25 | + for j in i: |
| 26 | + final_files.append(j) |
| 27 | + else: |
| 28 | + final_files.append(i) |
| 29 | + return final_files |
| 30 | + |
| 31 | +def percent(files, ext): |
| 32 | + summary = dict() |
| 33 | + count = 0 |
| 34 | + result = float() |
| 35 | + for i in files: |
| 36 | + try: |
| 37 | + if i[i.index("."):]== ext: |
| 38 | + count = count+1 |
| 39 | + else: |
| 40 | + if i[i.index("."):] in list(summary.keys()): |
| 41 | + summary[i[i.index("."):]] +=1 |
| 42 | + else: |
| 43 | + summary[i[i.index("."):]] = 1 |
| 44 | + except: continue |
| 45 | + try: |
| 46 | + result = round(count*100/len(files), 4) |
| 47 | + if count is 0: |
| 48 | + print("WARNING:number of files found with this extension is 0. Check you extension.") |
| 49 | + return result, len(files), count, summary |
| 50 | + except: |
| 51 | + print("0 files found. Check your path.") |
| 52 | + return None |
| 53 | + |
| 54 | + |
| 55 | + |
| 56 | + |
| 57 | +if __name__ == "__main__": |
| 58 | + validate_extension() |
| 59 | + files = preprocess() |
| 60 | + result = percent(files, extension) |
| 61 | + if str(type(result))=="<class 'tuple'>": |
| 62 | + print("==============EXTENSION SUMMARY===================") |
| 63 | + print(f"{extension} files :\t",result[2]) |
| 64 | + print("Total files :\t", result[1]) |
| 65 | + print("\n---------------------------") |
| 66 | + print("Percentage :\t", result[0],"%") |
| 67 | + print("---------------------------") |
| 68 | + if flag.lower() == "n": |
| 69 | + print("==================================================") |
| 70 | + else: |
| 71 | + others = result[3] |
| 72 | + print("\n=====Other Files=====") |
| 73 | + for i, j in zip(others.keys(), others.values()): |
| 74 | + print(f"{i} :\t{j}") |
| 75 | + print("==================================================") |
0 commit comments