source: trunk/essentials/dev-lang/python/Lib/dircache.py@ 3393

Last change on this file since 3393 was 3225, checked in by bird, 19 years ago

Python 2.5

File size: 1006 bytes
Line 
1"""Read and cache directory listings.
2
3The listdir() routine returns a sorted list of the files in a directory,
4using a cache to avoid reading the directory more often than necessary.
5The annotate() routine appends slashes to directories."""
6
7import os
8
9__all__ = ["listdir", "opendir", "annotate", "reset"]
10
11cache = {}
12
13def reset():
14 """Reset the cache completely."""
15 global cache
16 cache = {}
17
18def listdir(path):
19 """List directory contents, using cache."""
20 try:
21 cached_mtime, list = cache[path]
22 del cache[path]
23 except KeyError:
24 cached_mtime, list = -1, []
25 mtime = os.stat(path).st_mtime
26 if mtime != cached_mtime:
27 list = os.listdir(path)
28 list.sort()
29 cache[path] = mtime, list
30 return list
31
32opendir = listdir # XXX backward compatibility
33
34def annotate(head, list):
35 """Add '/' suffixes to directories."""
36 for i in range(len(list)):
37 if os.path.isdir(os.path.join(head, list[i])):
38 list[i] = list[i] + '/'
Note: See TracBrowser for help on using the repository browser.