Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8347359
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T07:20:25+00:00 2026-06-09T07:20:25+00:00

I’m new to Python and I have problem with this code: def dirs(currentDir): exe

  • 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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T07:20:27+00:00Added an answer on June 9, 2026 at 7:20 am

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a view passing on information from a database: def serve_article(request, id): served_article
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have a jquery bug and I've been looking for hours now, I can't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.