blob: 28aa6671295ed563695b65221ea8b6e8c686853c [file] [log] [blame]
Georg Brandl8ec7f652007-08-15 14:28:01 +00001
2:mod:`dircache` --- Cached directory listings
3=============================================
4
5.. module:: dircache
6 :synopsis: Return directory listing, with cache mechanism.
7.. sectionauthor:: Moshe Zadka <moshez@zadka.site.co.il>
8
9
10The :mod:`dircache` module defines a function for reading directory listing
11using a cache, and cache invalidation using the *mtime* of the directory.
12Additionally, it defines a function to annotate directories by appending a
13slash.
14
15The :mod:`dircache` module defines the following functions:
16
17
18.. function:: reset()
19
20 Resets the directory cache.
21
22
23.. function:: listdir(path)
24
25 Return a directory listing of *path*, as gotten from :func:`os.listdir`. Note
26 that unless *path* changes, further call to :func:`listdir` will not re-read the
27 directory structure.
28
29 Note that the list returned should be regarded as read-only. (Perhaps a future
30 version should change it to return a tuple?)
31
32
33.. function:: opendir(path)
34
35 Same as :func:`listdir`. Defined for backwards compatibility.
36
37
38.. function:: annotate(head, list)
39
40 Assume *list* is a list of paths relative to *head*, and append, in place, a
41 ``'/'`` to each path which points to a directory.
42
43::
44
45 >>> import dircache
46 >>> a = dircache.listdir('/')
47 >>> a = a[:] # Copy the return value so we can change 'a'
48 >>> a
49 ['bin', 'boot', 'cdrom', 'dev', 'etc', 'floppy', 'home', 'initrd', 'lib', 'lost+
50 found', 'mnt', 'proc', 'root', 'sbin', 'tmp', 'usr', 'var', 'vmlinuz']
51 >>> dircache.annotate('/', a)
52 >>> a
53 ['bin/', 'boot/', 'cdrom/', 'dev/', 'etc/', 'floppy/', 'home/', 'initrd/', 'lib/
54 ', 'lost+found/', 'mnt/', 'proc/', 'root/', 'sbin/', 'tmp/', 'usr/', 'var/', 'vm
55 linuz']
56