title: Read all files in directory and subdirectories in Python
body: <p>I'm trying to translate this bash line in python:</p>
  
  <pre><code>find /usr/share/applications/ -name "*.desktop" -exec grep -il "player" {} \; | sort | while IFS=$'\n' read APPLI ; do grep -ilqw "video" "$APPLI" &amp;&amp; echo "$APPLI" ; done | while IFS=$'\n' read APPLI ; do grep -iql "nodisplay=true" "$APPLI" || echo "$(basename "${APPLI%.*}")" ; done
  </code></pre>
  
  <p>The result is to show all the videos apps installed in a Ubuntu system.</p>
  
  <p>-> read all the .desktop files in /usr/share/applications/ directory</p>
  
  <p>-> filter the strings "video" "player" to find the video applications</p>
  
  <p>-> filter the string "nodisplay=true" and "audio" to not show audio players and no-gui apps</p>
  
  <p>The result I would like to have is (for example):</p>
  
  <pre><code>kmplayer
  smplayer
  vlc
  xbmc
  </code></pre>
  
  <p>So, I've tried this code:</p>
  
  <pre><code>import os
  import fnmatch
  
  apps = []
  for root, dirnames, filenames in os.walk('/usr/share/applications/'):
     for dirname in dirnames:
       for filename in filenames:
          with open('/usr/share/applications/' + dirname + "/" + filename, "r") as auto:
              a = auto.read(50000)
              if "Player" in a or "Video" in a or "video" in a or "player" in a:
                if "NoDisplay=true" not in a or "audio" not in a:
                    print "OK: ", filename
                    filename = filename.replace(".desktop", "")
                    apps.append(filename)
  
  print apps
  </code></pre>
  
  <p>But I've a problem with the recursive files...</p>
  
  <p>How can I fix it?
  Thanks</p>