954,525 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Recursive file and folder tree

I would really need some help :(

Write a recursive method, as a parameter to accept the folder name (absolute or relative to the current folder) that prints lists of all the files and all folders in the folder, which is given in parameter, and displays all folders in all subfolders, ... Output, the entire tree structure of files and folders.
Before each folder should be "*", before each file "-". From the shift should be apparent how deep into the tree, the file / folder is located.

For example:

>>> write_files('Desktop')

- picture.txt
* Songs
   * Romantic songs
      - Do you love me.mp3
      - When we meet.mp3 
   - heart.jpg
* Movies
   - The kid.xvid
Binika
Newbie Poster
7 posts since Nov 2010
Reputation Points: 14
Solved Threads: 0
 

I've tried something...

import os

def write_files(file):
    for element in os.listdir(file):
        if os.path.isdir(element):
            print('*' + element)
            write_files(element)
        else:
            print('-' + element)
Binika
Newbie Poster
7 posts since Nov 2010
Reputation Points: 14
Solved Threads: 0
 

Playing little with my code snippet could give some ideas how to proceed (but you need not open the files like I did to find the text in them)

pyTony
pyMod
Moderator
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
 

You did such a good try that I fixed it to work:

import os
step = 3

def write_files(file, indent=0):
    for element in os.listdir(file):
        if os.path.isdir(element):
            print(indent*' '+'*' + element)
            write_files(os.path.join(file,element), indent+step)
        else:
            print(indent*' '+'-' + element)
write_files('.')
pyTony
pyMod
Moderator
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
 

Nice job but it still doesn't work like I want to. For example... on my desktop I have a file Pictures. In it, there are files Seaside, Wedding and New Year. And in all of them there are pictures. I want that this pictures would also be printed on my screen. Now it prints only *Pictures and then -Seaside -Wedding -New Year. But these files (Seaside, Wedding,...) should also be written with * because they're files :) Do you understand what I mean?
I really thought I understand recursion... but when I got this method to write, my whole knowledge disappeared :(

Binika
Newbie Poster
7 posts since Nov 2010
Reputation Points: 14
Solved Threads: 0
 

I think you need a GUI script. That will be more handy.

Writting a recursive script to recurse through your entire file system is not that good on resources.

Think about it.
;)

richieking
Master Poster
764 posts since Jun 2009
Reputation Points: 61
Solved Threads: 152
 

I noticed after posting that you got the logic little messed up, but you get the right idea how the recursion works. But you have to have basic case first, the files should be printed for the file list and only recurse to the directories. Maybe better to use os.path.walk with for. Then you do not need to reimplement the walk. (If it is not required by others)

pyTony
pyMod
Moderator
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
 

I've made it :)

import os
step = 3

def write_files(file, indent=0):
    for element in os.listdir(file):
        if os.path.isdir(file+'\\'+element):
            print(indent*' '+'*' + element)
            write_files(os.path.join(file,element), indent+step)
        else:
            print(indent*' '+'-' + element)
write_files('.')


There was just something missing in os.path.isdir() :)

Binika
Newbie Poster
7 posts since Nov 2010
Reputation Points: 14
Solved Threads: 0
 

Nice debugging! Just use os.path.join also at line 6.

pyTony
pyMod
Moderator
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: