I am attempting to count all files in a directory and any sub directories by type and over all size
The output should be a table that looks something like:
Directory A
Number of subdirectories: 12
|Type| Count| TotalSize/kb |FirstSeen |LastSeen |
|----|-------|------------------|-----------|----------|
|.pdf| 8 |80767 |1/1/2020 |2/20/2020 |
|.ppt| 9 |2345 |1/5/2020 |2/25/2020 |
|.mov| 2 |234563 |1/10/2020 |3/1/2020 |
|.jpg| 14 |117639 |1/15/2020 |3/5/2020 |
|.doc| 5 |891 |1/20/2020 |3/10/2020 |
Sorry i was trying to get this into a table format for readability. But each record starts with a file type found in the directory.
This should do exactly what you want, count the size mapped by extensions. Tidy it up, pretty print the way you like, and you are done.
import os
def scan_dir(root_dir):
# walk the directory
result = {}
for root, dirs, files in os.walk(root_dir):
# count the files size
for file in files:
path = os.path.join(root, file)
ext = os.path.splitext(file)[1].upper()
size = os.stat(path).st_size
result[ext] = (result[ext] if ext in result else 0) + size
return result
print(scan_dir("."))
Edit: This doesn't collect the min/max timestamps for you, nor counts the files, but this should really put you on the right track.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.