-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_directory_structure.py
More file actions
111 lines (86 loc) · 3.35 KB
/
get_directory_structure.py
File metadata and controls
111 lines (86 loc) · 3.35 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# get_directory_structure.py
# Generates text file of all files and folders in specified directory
# Usage: list_directory_names_harmy.py <directory to be searched>
import os # Used for getting directory information
import sys # Used for sys.exit and arguments
import io # Used to avoid encoding errors
# Global variables for number of directories and files, and filesizes
directory_count = 0
file_size = 0
file_count = 0
# Recursive function which searches through directories
def traverse(directory, depth):
# Setting global to make sure counts are updated
global directory_count, file_count, file_size
# Make sure directory is accessible
try:
files = os.listdir(directory)
except Exception as err:
print("Could not access: " + directory)
return
# Iterate through list and add all files to report
for file in files:
# Ignore if file ends in .ini or .db
if ".ini" in file or ".db" in file:
continue
# Adding formatting
for i in range(0, depth):
report_write("\t")
# Getting full path
full_path = directory + "\\" + file
# If it's a directory, print to report and traverse further
if os.path.isdir(full_path):
report_write(file + "\n")
directory_count += 1
traverse(full_path, depth + 1)
# If it's a file, add file size
elif os.path.isfile(full_path):
report_write(file + "\n")
file_count += 1
file_size += os.path.getsize(full_path)
# Add new line to separate
report_write("\n")
# Function to convert bytes to human readable formats
def get_human_readable(size):
suffixes = ['B', 'KB', 'MB', 'GB', 'TB']
suffixIndex = 0
while size > 1024 and suffixIndex < 4:
suffixIndex += 1 # increment the index of the suffix
size = size / 1024.0 # apply the division
return "%.2f%s" % (size, suffixes[suffixIndex])
# Function to write to file
def report_write(line):
report = io.open(os.getcwd() + "/report.txt", "a", encoding="utf-8")
report.write(line)
report.close()
# Main function
def main():
print("get_directory_structure.py running")
# Ensuring that arguments are correct
if len(sys.argv) != 2:
print("Improper number of arguments.")
print("Usage: list_directory_names_harmy.py <directory to be searched>")
sys.exit(1)
# Create file in current working directory
report = io.open(os.getcwd() + "/report.txt", "w", encoding="utf-8")
report.close()
# If final backslash is present, get rid of it
directory = sys.argv[1]
if directory.endswith("\\"):
directory = directory[:-1]
# Making sure directory is valid
if not os.path.exists(sys.argv[1]):
print("Invalid directory entered")
sys.exit(1)
# Calling traverse function
traverse(directory, 0)
# Printing stats
print("Total Files:\t\t" + str(file_count))
print("Total Directories:\t" + str(directory_count))
print("Total Size:\t\t" + get_human_readable(file_size))
print("Check report.txt for list of all files and directories found")
# Ending program
sys.exit(0)
# Needed for main function
if __name__ == "__main__":
main()