Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think you can fix this by passing the full path name into <code>printRootStructure</code>:</p> <pre><code>import os def printRootStructure(dirname,indent=0): for i in range(indent): print " ", print os.path.basename(dirname) # changed if os.path.isdir(dirname): for files in os.listdir(dirname): printRootStructure(os.path.join(dirname,files),indent+1) # changed </code></pre> <p>As it was in your original code, you were passing just the last part (this is called the "basename") of each file into <code>printRootStructure</code> when you made the recursive calls.</p> <h3>Working directory and path names</h3> <p>Any time you start up a program on a modern computer, your program runs in a fixed location in the filesystem (this is called the "current working directory"). If you invoke a program from the commane-line, the current working directory is simply the path where you invoked the program's name. If you invoke a program by clicking something in a GUI environment, it can get more complicated, but the general behavior is the same: your program always runs in a specific working directory.</p> <p>All path tests, and in particular <a href="http://docs.python.org/2/library/os.path.html" rel="nofollow"><code>os.path.isdir</code></a>, are evaluated with respect to that working directory. So when you make your first recursive call in your example, you are testing <code>os.path.isdir("test1")</code>, which doesn't exist in the working directory -- it only exists inside "test" !</p> <p>The fix is to pass the full path name into your recursive calls. Then, because your full name might be excessive when you print out the tree, I added a call to <a href="http://docs.python.org/2/library/os.path.html" rel="nofollow"><code>os.path.basename</code></a> to print just the basename portion of each file.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload