forked from Logan1x/Python-Scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfindlargefiles.py
More file actions
66 lines (56 loc) 路 2.49 KB
/
Copy pathfindlargefiles.py
File metadata and controls
66 lines (56 loc) 路 2.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
# findlargefiles.py Searches a file location and subdirectories for
# files larger than a given size.
"""
findlargefiles.py Searches a file location and subdirectories for
files larger than a given size.
Useful for phones which might hide files in FileExplorer.
Directly prints results if run directly.
May also be imported, yielding results one by one.
Created on Sun Sep 3 20:35:12 2017
@author: david.antonini // toonarmycaptain
"""
import os
def search_folder(location, min_file_size):
file_not_found_errors_count = 0
files_found_count = 0
total_size = 0
print(f'Files larger than {min_file_size:.2f} MB in location: {location}')
for foldername, subfolders, filenames in os.walk(location):
for filename in filenames:
try:
actual_size = os.path.getsize(os.path.join(foldername,
filename))
if min_file_size*1024**2 <= actual_size:
print(f'{foldername}\\{filename} - '
f'{(actual_size/1024**2):.2f} MB')
yield foldername, filename, actual_size
files_found_count += 1
total_size += actual_size
except FileNotFoundError:
file_not_found_errors_count += 1
print(f'FileNotFoundError: {filename}')
print(f'Files found: {files_found_count}')
print(f'Total size: {(total_size/1024**2):.2f} MB')
if file_not_found_errors_count > 0:
print(f'FileNotFoundErrors: {file_not_found_errors_count}')
if __name__ == '__main__':
print('This program searches for files larger than a given size '
'in a given location.')
while True:
location = input("Where would you like to search? ")
if os.path.exists(location):
break
else:
print('Please enter a valid path.')
while True:
try:
min_file_size = float(input('Please enter file size in MB: '))
break
except ValueError:
print('Please enter numeric input only.')
search_folder(location, min_file_size)
for foldername, filename, actual_size in search_folder(location,
min_file_size):
print(f'{foldername}\\{filename} - {(actual_size/1024**2):.2f} MB')