Reputation: 7
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!
Upvotes: 0
Views: 164
Reputation: 16905
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
should return
.--q
, you will see should return
once more--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
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
Upvotes: 1
Reputation: 2441
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().
Upvotes: 2