At the release of 1.0.1.
[python/dscho.git] / Lib / dircache.py
blobef68b429f97c3f27687f187785154fd1905e814a
1 # Module 'dircache'
3 # Return a sorted list of the files in a directory, using a cache
4 # to avoid reading the directory more often than necessary.
5 # Also contains a subroutine to append slashes to directories.
7 import os
9 cache = {}
11 def listdir(path): # List directory contents, using cache
12 try:
13 cached_mtime, list = cache[path]
14 del cache[path]
15 except KeyError:
16 cached_mtime, list = -1, []
17 try:
18 mtime = os.stat(path)[8]
19 except os.error:
20 return []
21 if mtime <> cached_mtime:
22 try:
23 list = os.listdir(path)
24 except os.error:
25 return []
26 list.sort()
27 cache[path] = mtime, list
28 return list
30 opendir = listdir # XXX backward compatibility
32 def annotate(head, list): # Add '/' suffixes to directories
33 for i in range(len(list)):
34 if os.path.isdir(os.path.join(head, list[i])):
35 list[i] = list[i] + '/'