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

Recommended Answers

All 8 Replies

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)

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)

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('.')

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 :(

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.
;)

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)

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() :)

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.