Nov 24, 2024

Wiki

Python

Aide

edit SideBar

Search

Dircache

Source : PyMOTW de Doug Hellmann.

Présentation

Le module dircache contient une fonction pour mettre en cache des listings de répertoires. Ces listings sont mis à jours dès qu'une date de dernière modification d'un répertoire a changé.

Principe

La fonction principale de ce module est listdir(), une surcouche de os.listdir() : elle met en cache les résultats de os.listdir, i.e. la liste des éléments du répertoire passé en argument.

A chaque fois que l'on appellera dircache.listdir, cette liste sera renvoyée, sauf si la date de dernière modification du répertoire passé en argument à changé.

Exemple

  >>> import dircache

  >>> chemin = '.'
  >>> premier  = dircache.listdir(chemin)
  >>> deuxieme = dircache.listdir(chemin)

On affiche le contenu de premier, pour se rendre compte que dircache.listdir retourne bien la même chose que os.listdir :

  >>> print 'Contenu :', premier
  Contenu : ['Bibliotheque', 'Boulot', 'Cours', 'Scripts', 'These', 'savoir.txt']

On vérifie alors que premier et deuxieme sont identiques et égaux :

  >>> print 'Identiques : ',premier is deuxieme
  Identiques :  True

  >>> print 'Egaux : ',premier == deuxieme
  Egaux :  True

It is important to recognize that the exact same list is returned each time, so it should not be modified in place.

Of course, if the contents of the directory changes it is rescanned.

import dircache import os

path = '/tmp' file_to_create = os.path.join(path, 'pymotw_tmp.txt')

  1. Look at the directory contents

first = dircache.listdir(path)

  1. Create the new file

open(file_to_create, 'wt').close()

  1. Rescan the directory

second = dircache.listdir(path)

  1. Remove the file we created

os.unlink(file_to_create)

print 'Identical :', first is second print 'Equal :', first == second print 'Difference:', list(set(second) - set(first))

In this case the new file causes a new list to be constructed.

$ python dircache_listdir_file_added.py Identical : False Equal : False Difference: ['pymotw_tmp.txt']

It is also possible to reset the entire cache, discarding its contents so that each path will be rechecked.

import dircache

path = '/tmp' first = dircache.listdir(path) dircache.reset() second = dircache.listdir(path)

print 'Identical :', first is second print 'Equal :', first == second print 'Difference:', list(set(second) - set(first))

$ python dircache_reset.py Identical : False Equal : True Difference: []

Annotated Listings:

The other interesting function provided by the dircache module is annotate(). When called, annotate() modifies a list such as is returned by listdir(), adding a '/' to the end of the names that represent directories. (Sorry Windows users, although it uses os.path.join() to construct names to test, it always appends a '/', not os.sep.)

import dircache from pprint import pprint

path = '../../trunk'

contents = dircache.listdir(path)

annotated = contents[:] dircache.annotate(path, annotated)

fmt = '%20s\t%20s'

print fmt % ('ORIGINAL', 'ANNOTATED') print fmt % (('-' * 20,)*2)

for o, a in zip(contents, annotated):

    print fmt % (o, a)

$ python dircache_annotate.py

            ORIGINAL	           ANNOTATED

--------------------
           .DS_Store	           .DS_Store
                .svn	               .svn/
           ChangeLog	           ChangeLog
         LICENSE.txt	         LICENSE.txt
            MANIFEST	            MANIFEST
         MANIFEST.in	         MANIFEST.in
      MANIFEST.in.in	      MANIFEST.in.in
            Makefile	            Makefile
              PyMOTW	             PyMOTW/
          README.txt	          README.txt
         setup.py.in	         setup.py.in
      static_content	     static_content/
       template.html	       template.html

Page Actions

Recent Changes

Group & Page

Back Links