0

I'm looking for effective way to go to every folder (including subfolder) in my directory list. I then need to run some processes on that folder (like size, number of folders and files etc.).

I know that I have 2 options for that: - Recurrence (my current implementation, code below) - At start of program generating list of all folders and invoking my function in look

I know that my current implementation is not perfect can somebody take a look on it and possibly advise any updates. In addition can somebody help me howto (I'm assuming using os.path library) generate list of all folder including subfolders ?

My current code that analyse folder (using recurrence):

def analyse_folder(path, resultlist=[]):
 # This is trick to check are we in last directory
 subfolders = fsprocess.get_subdirs(path)
 for subfolder in subfolders:
 analyse_folder(subfolder, resultlist)
 files, dirs = fsprocess.get_numbers(subfolder)
 size = fsprocess.get_folder_size(subfolder)
 resultlist = add_result([subfolder, size, files, dirs], resultlist)
 return resultlist

This is the code that getting list of subfolders inside folder:

def get_subdirs(rootpath, ignorelist=[]):
 # We are starting with empty list
 subdirs = []
 # Generate main list
 for path in os.listdir(rootpath):
 # We are only interested in dirs and thins not from ignore list
 if not os.path.isfile(os.path.join(rootpath, path)) and path not in ignorelist:
 subdirs.append(os.path.join(rootpath, path))
 # We are giving back list of subdirectories
 return subdirs

And this is simple function to add it to resullist:

def add_result(result, main_list):
 main_list.append(result)
 return main_list

So if anyone can: 1) Tell me is my attitude is good 2) Provide me code to generate list of all of directories in given folder (for example everything under C:\users)

Thank you

Artsiom Rudzenka
29.3k5 gold badges36 silver badges53 bronze badges
asked Sep 1, 2015 at 8:28

1 Answer 1

4

Try os.walk:

import os
for (root, dirs, files) in os.walk(somefolder):
 # root is the place you're listing
 # dirs is a list of directories directly under root
 # files is a list of files directly under root
answered Sep 1, 2015 at 8:31
Sign up to request clarification or add additional context in comments.

2 Comments

Hello, thank you - this is list of directories directly above root - I want to get directories above this subdirs , and above and above - if you now what I mean (everyt folder path that is under root)
When you do an iteration for (root, dirs, files) in os.walk("/home/someuser"), it will start listing /home/someuser and recurse all the way until there are no deeper levels.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.