Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello,
I'm writing a program using python on ubuntu 18.04.
The goal of this program is to list all files on the machine in every directory.
I'm using a for loop and recursive programming to achieve this goal.
I write the code below:
Python
def listDirectory(RootPath):
    os.chdir(RootPath)

    print(
        '\n\033[31mCurrently at \033[37m'+
        os.getcwd())

    currentpaths = os.listdir()

    pathList = [RootPath,currentpaths]        
    print('\n\033[32m pathList: \033[37m')
    print(pathList)
    AllPaths.append(pathList)
    
    for path in currentpaths:
        # if the path is a directory crawl it
        # if the path is not a directory go for the next item
        try:
            listDirectory(path)
        except:
            pass


the problem is that...

It goes true the first directory that it sees.
But it doesn't go back for the next directory.
It doesn't recur as I'm expecting.
So it gives me only three pathLists which are the first possible routs it finds.

given the RootPath of
Python
/

it just goes these Directories
Python
/

Python
/lib

Python
/lib/hdparm


It doesn't return other addresses, and ends very quickly.


In advance I thank you for your time.

What I have tried:

I'm looking for the problem in my recursive algorithm.
Posted
Updated 8-Jul-20 1:59am
Comments

The recursive function should not return with your system in a different state, everything should be as it was before. Where do you undo chdir?
 
Share this answer
 
v2
Comments
Richard MacCutchan 8-Jul-20 9:22am    
Well spotted.
m.r.m.40 8-Jul-20 9:30am    
Yes. Thats the answer.
Thank you.
If your code doesn't work - and that happens a lot, even when you have experience - you need to learn to find out why yourself, so you can fix it. Out telling you what it wrong doesn't teach you anything about how to avoid making mistakes in future.

So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. How you use it depends on your compiler system, but a quick Google for the name of your IDE and "debugger" should give you the info you need.
Learn to use it - it's the most powerful tool in your box, and you will probably spend more time using it than the editor - we all do!

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
 
Share this answer
 
Comments
m.r.m.40 8-Jul-20 9:28am    
You're right. Thank you. :)
Was the last line necessary? :D
Quote:
I'm looking for the problem in my recursive algorithm.

Watching your code performing is an incredible way to learn how the code works, or don't. The tools of choice is the debugger.
-----
Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

27.3. pdb — The Python Debugger — Python 3.6.1 documentation[^]
Debugging in Python | Python Conquers The Universe[^]
pdb – Interactive Debugger - Python Module of the Week[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 
v2
Comments
m.r.m.40 8-Jul-20 9:29am    
:D :D
OK! I got it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900