how to loop through certain directories in a directory structure in python?
<p>I have the following directory structure</p>

<p><a href="http://i.stack.imgur.com/AR4Sv.png" rel="nofollow"><img src="http://i.stack.imgur.com/AR4Sv.png" alt="enter image description here"></a></p>

<p><a href="http://i.stack.imgur.com/vEnMe.png" rel="nofollow"><img src="http://i.stack.imgur.com/vEnMe.png" alt="enter image description here"></a></p>

<p>As you can see in the pics, there are a lot of .0 files in different directories. This directory structure exists for 36 folders(Human_C1 to C36) and each Human_C[num] folder has a 1_image_contours folder which has a contours folder with all related .0 files. </p>

<p>These .0 files contain some co-ordinates(x,y). I wish to loop through all these files, take the data in them and put it in an excel sheet(I am using pandas for this). </p>

<p>The problem is, how I loop through only these set of files and none else? (there can be .0 files in contour_image folders also)</p>

<p>Thanks in advance</p>

<p>Since your structure is not recursive I would recommend this:</p>

<pre><code>import glob
zero_files_list = glob.glob("spinux/generated/Human_C*/*/contours/*.0")
for f in zero_files_list:
    print("do something with "+f)
</code></pre>

<p>Run it from the parent directory of <code>spinux</code> or you'll have no match!</p>

<p>It will expand the pattern for the fixed directory tree above, just as if you used <code>ls</code> or <code>echo</code> in a linux shell.</p>

python file loops operating-system