0

I'm new to Python and I have problem with this code:

def dirs(currentDir):
    exe = True
    allDirs = os.listdir(currentDir)
    print "Directories in %s:" % currentDir
    for files in allDirs:
        print files
    direc = raw_input("Directory name?:")

    if direc == "--q":
        exe = False
    elif currentDir == "/" and exe == True:
        theDir =  currentDir + direc
        dirs(theDir)
    elif currentDir != "/" and exe == True:
        theDir = currentDir + "/" + direc
        dirs(theDir)
    print "should return"

Why, when I type --q, prints should return several times? If directory is /home/username/, it will print three times, if directory is /home/ it will print two times and so on. I also tried to return in if statement:

if direc == "--q":
    return something

But then nothing happens. Any ideas? Many thanks!

2
  • 3
    Welcome to Stack Overflow! Can you fix your indentation in the post? That would be very useful for reading Python, thanks. Commented Aug 5, 2012 at 9:28
  • 1
    what exactly is this code supposed to do? could you explain that? might help us help you... Commented Aug 5, 2012 at 9:38

2 Answers 2

2

This seems to me to be related to the recursiveness of your function. If you enter two directories while using your program, you would expect 3 "should return"s to be printed, since there are three calls to dirs().

Sign up to request clarification or add additional context in comments.

2 Comments

I thought there was problem with functions. But how I need to rewrite code that function would work normally?
@Taadas: The question is: What do you want your code to do exactly? The code I posted should do the exact same thing as yours, and it does work. The question is, does it do what you want?
1

From what I've understood from your comments, this should do the correct thing.

def dirs(currentDir):
    allDirs = os.listdir(currentDir)
    print "Directories in %s:" % currentDir
    for files in allDirs:
        print files
    direc = raw_input("Directory name?:")

    if direc != "--q":
        theDir =  os.path.join(currentDir, direc)
        return dirs(theDir)
    else:
        return currentDir

Discussion of the code as provided by you

Please add more information to your post what the code should do. Meanwhile, see here for a semantically equivalent function, it does the exact same thing as your function, but I have removed certain unnecessary things.

def dirs(currentDir):
    allDirs = os.listdir(currentDir)
    print "Directories in %s:" % currentDir
    for files in allDirs:
        print files
    direc = raw_input("Directory name?:")

    if direc != "--q":
        theDir =  os.path.join(currentDir, direc)
        dirs(theDir)
    print "should return"

Now, as long as you don't enter --q, it will never print "should return".

What is the exe variable for in your program? It doesn't do anything.

If the first if clause is executed, non of the others will be excuted, because if/elif/.../else are mutually exclusive clauses. Once you set exe = True, exe will never be accessed again. Thus, you can remove exe from your code – as it stands – entirely. Perhaps, though, you wanted it to do something different than preventing those elif clauses from executing.

As for should return

  • You will always see at least one should return.
  • For every time you don't enter --q, you will see should return once more
  • They all print after you enter --q, because it the print statement is after the recursive call.

Further, I replaced your directory name handling logic with os.path.join() which works across all platforms.

Here the current behavior:

>>> dirs(r"C:\Python27")
Directories in C:\Python27:
DLLs
Doc
include
Lib
libs
LICENSE.txt
NEWS.txt
python.exe
pythonw.exe
README.txt
Removesetuptools.exe
Scripts
setuptools-wininst.log
tcl
Tools
w9xpopen.exe
Directory name?:Doc
Directories in C:\Python27\Doc:
python273.chm
Directory name?:--q
should return
should return

Recursion

Compare these two functions to see the effect for handling output before and after the recursive call:

def string_foo(text):
    first, rest = text[0], text[1:]
    print first
    if rest:
        string_foo(rest)




def string_bar(text):
    first, rest = text[0], text[1:]
    if rest:
        string_foo(rest)
    print first

Output:

>>> string_foo("Hello")
H
e
l
l
o

>>> string_bar("Hello")
o
l
l
e
H

8 Comments

Thanks for the code and the tips! Sorry, I haven't noticed about print "should return", it remained from debugging. Instead of print "should return", should be return currentDir. Then You can browse through directories and if You type --q (which means quit), function would return directory in which you are. So If I type --q, I want to return directory in which I am.
@Taadas So you want to go one directory up?
It's like browsing through directories and when I type --q exit function by returning current directory in which I am.
In the example, I start in C:\Python27, then I enter the Doc directory. Then I type --q, would you like it to go back to C:\Python27?
@Taadas, forgot to mention you.
|

Your Answer

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.