I've progressed with the directory statistics function and want to proceed by making it a class.
Link to the first version of this code: Link
My current state of things:
About my namings:
I first used the name fcount
as I wanted to have it stand for files and folders as otherwise, the name would be too long. That's why I made the exception to shorten it. I'm still continuing with your more experienced solution for this.
I wrote pathlib
into the function name because I have the same function above it with os.walk
as this was my first way to try. But os.walk
seem to have problems scanning my network drive as it always returned 0 bytes. Therefore I've chosen pathlib
. Hope that makes sense.
About my classes: I'm starting to feel comfortable programming python but as soon as I start to use classes my whole code starts to fall apart and seems to have to be more complex. I know that's just a beginner problem, but as I usually can't solve the issues appearing, I'm careful with that route. I've now rewritten it into a class but facing a few problems now.
I started to try to structure it with the tips from the first CodeReview by writing the file search for-loop into the __init__
function but python was then saying it can't return a value from __init__
so I created a new method named def get_directory_statistics(self, scan_path):
.
I'm not sure where to input my scan_path
, into the __init__
or the first method def get_directory_statistics(self, scan_path):
.
Your advice to summarize two lines into one, sadly didn't work for me either return size_and_file_count(size_gb, all_types_count, file_count, folder_count)
. I couldn't get it to work. It's always saying size_and_file_count
is not defined or other Errors.
Optimizing the code: I outlined above why I sadly can't use os.walk for this. So this won't work for me. And C seems at the moment, not like an option as the only programming language I am familiar with is python and I guess it would be a more complex task to program a wrapper and the code itself in C
. I think most of it will be I/O bound, yes.
Again I learned a lot from the first answer here on CodeReview!
Below you'll find my solution after going over all the last notes
class get_size_and_file_count:
"""Gets the total size of a given dir and counts how many folders and files are in the given
path directory and return a file count, folder count and all non hidden files as a sum"""
def __init__(self, total_size = 0, non_hidden_files_count = 0, file_count = 0, folder_count = 0):
self.total_size = total_size
self.non_hidden_files_count = non_hidden_files_count
self.file_count = file_count
self.folder_count = folder_count
def get_directory_statistics(self, scan_path):
self.root_directory = Path(scan_path)
for f in self.root_directory.glob('**/*'):
if f.is_file():
self.file_count += 1
self.total_size += f.stat().st_size
if not f.name.startswith("."):
self.non_hidden_files_count += 1
if f.is_dir():
self.folder_count += 1
directory_statistics = [self.total_size, self.non_hidden_files_count, self.file_count, self.folder_count]
return directory_statistics
def print_directory_statistics(self):
print('Directory path to search: {}'.format(self.root_directory))
print('Directory size in GB: {:.2f}GB'.format(self.total_size / 1.0e9))
print('Amount of non hidden files: {}'.format(self.non_hidden_files_count))
print('Amount of files searched: {}'.format(self.file_count))
print('Amount of folders searched: {}'.format(self.folder_count))
result = get_size_and_file_count()
directory_statistics = result.get_directory_statistics("...") # Store directory statistics in var
result.print_directory_statistics() # Print directory statistics
print_result = ...
, just writeresult.print_directory_statistics()
. Indeed, please make sure the code works, then we can review it properly. \$\endgroup\$