Recorrer árbol de directorios en Python

Minireceta : Ejemplo de cómo recorrer un árbol de directorios y realizar tareas sobre ficheros o directorios del mismo.


Recorrer todos los directorios bajo un path

import os
for base, dirs, files in os.walk('/home/user'):
  print base


Recorrer todos los ficheros bajo un path

import os
for base, dirs, files in os.walk('/home/user'):
  print files


Listar el tamaño en bytes de cada archivo de un path

import os
from os.path import join, getsize
for root, dirs, files in os.walk('python/Lib/email'):
   print root, "consumes",
   print sum(getsize(join(root, name)) for name in files),
   print "bytes in", len(files), "non-directory files"
   if 'CVS' in dirs:
       dirs.remove('CVS')  # don't visit CVS directories


Excluír directorios de os.walk

#(Posterior al os.walk):
 
for d in dirs[:]:
 if os.path.join(base, d) == EXCLUDED_DIR
  dirs.remove(d)


Borrar todos los ficheros y subdirectorios de un directorio

# Delete everything reachable from the directory named in 'top',
# assuming there are no symbolic links.
# CAUTION:  This is dangerous!  For example, if top == '/', it
# could delete all your disk files.
import os
for root, dirs, files in os.walk(top, topdown=False):
   for name in files:
       os.remove(os.path.join(root, name))
   for name in dirs:
       os.rmdir(os.path.join(root, name))


Listar sólo el primer nivel de directorios

# De un directorio especifico:
 
[ name for name in os.listdir(thedir) if os.path.isdir(os.path.join(thedir, name)) ]
 
# Del directorio actual:
 
 directories=[d for d in os.listdir(os.path.curdir) if os.path.isdir(d)]
 
# o bien:
 
filter (os.path.isdir, os.listdir(os.getcwd()))`
 
# Funciones listas para usar:
 
def listdirs(folder):
   return [d for d in os.listdir(folder) if os.path.isdir(os.path.join(folder, d))]
 
def listdirs_fullnames(folder):
   return [
       d for d in (os.path.join(folder, d1) for d1 in os.listdir(folder))
       if os.path.isdir(d)
   ]


Uso de os.walk

Esta solución no sigue enlaces simbólicos.

#!/usr/bin/python
 
import os
basedir= '/home/sromero/python'
 
#---------------------------------------------------
# Act over all files in the current directory
#---------------------------------------------------
def CheckFilesInDirectory(s1, directory, filelist):
   #print s2 # current dir
   for file in filelist:
      DoSomethingWithFile( directory, file )
 
#---------------------------------------------------
def DoSomethingWithFile( path, file ):
   fullpath = os.path.join(path, file)
   print fullpath
 
os.path.walk(basedir, CheckFilesInDirectory, 'somenull')


Uso de os.listdir

Utiliza una función de callback.

#!/usr/bin/python
import os, sys
from stat import *
 
def walktree(top, callback):
   '''recursively descend the directory tree rooted at top,
      calling the callback function for each regular file'''
 
   for f in os.listdir(top):
       pathname = os.path.join(top, f)
       mode = os.stat(pathname)[ST_MODE]
       if S_ISDIR(mode):
           # It's a directory, recurse into it
           walktree(pathname, callback)
       elif S_ISREG(mode):
           # It's a file, call the callback function
           callback(pathname,f,top)
       else:
           # Unknown file type, print a message
           print 'Skipping %s' % pathname
 
def visitfile(fullname,file,path):
   print 'visiting', file, 'in', path, "=", fullname
 
if __name__ == '__main__':
   walktree(sys.argv[1], visitfile)


<Volver a la sección de Tutoriales Python>