-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDirectory File Explorer.py
48 lines (36 loc) · 1.45 KB
/
Directory File Explorer.py
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
# -*- coding: utf-8 -*-
"""Python 102 - Assignment 13
Automatically generated by Colab.
Original file is located at
https://colab.research.google.com/drive/1p094t88bT3c3LK8qkC3Okc6UjsTk-R79
"""
import os
def calculate_directory_stats(directory_path):
# Initialize counters
num_directories = 0
num_files = 0
total_size = 0
# Walk through the directory and its subdirectories
for root, dirs, files in os.walk(directory_path):
num_directories += len(dirs) # Count directories
num_files += len(files) # Count files
# Calculate total size of files
for file in files:
file_path = os.path.join(root, file)
total_size += os.path.getsize(file_path)
return num_directories, num_files, total_size
def main():
# Input the directory path from the user
directory_path = input("Enter the directory path: ")
# Check if the provided path is a valid directory
if os.path.exists(directory_path) and os.path.isdir(directory_path):
# Calculate directory statistics
num_directories, num_files, total_size = calculate_directory_stats(directory_path)
# Display the results
print(f"Number of Directories: {num_directories}")
print(f"Number of Files: {num_files}")
print(f"Total Size of Files: {total_size} bytes")
else:
print("Invalid directory path. Please provide a valid directory.")
if __name__ == "__main__":
main()