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.

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

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

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

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

# 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))

# 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)

Esta solución no sigue enlaces simbólicos.

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')

Utiliza una función de callback.

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
           # 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>

  • programacion/tutoriales/python/recorrer_arbol.txt
  • Última modificación: 02-02-2009 22:17
  • por sromero