File System List class: a subclass of list designed for manipulating objects in the file system. flist = FSList([pattern = '', [recursive = False, [file_only = False]]]) - pattern can be a string or a list of files and subdirectories. It can also contain wildcards (*, ? or [
| 22 | COMPAT23 = False |
| 23 | |
| 24 | class FSList(list): |
| 25 | """ |
| 26 | File System List class: a subclass of list designed for |
| 27 | manipulating objects in the file system. |
| 28 | |
| 29 | flist = FSList([pattern = '', [recursive = False, [file_only = False]]]) |
| 30 | |
| 31 | - pattern can be a string or a list of files and subdirectories. It can |
| 32 | also contain wildcards (*, ? or []), in which case the pattern |
| 33 | will be expanded automatically using glob.glob() |
| 34 | - recursive controls whether files matching pattern in subdirectories |
| 35 | are added to the list. |
| 36 | - file_only is a convenience option for when you just want a list |
| 37 | of the regular files matching the patter (i.e., no directories, |
| 38 | special files like devices). |
| 39 | - It doesn't follow links or symlinks. At least I don't think it does. |
| 40 | - Computed properties: size (total size of all regular files, in bytes) |
| 41 | smallest (smallest item) |
| 42 | largest |
| 43 | newest (most recent modification time) |
| 44 | oldest (oldest modification time) |
| 45 | |
| 46 | The same init options are also available on extend and append methods. |
| 47 | |
| 48 | flist.extend('./*.c', recursive=True, file_only=True) |
| 49 | |
| 50 | Extends the list with all C files in the current directory and |
| 51 | any subdirectories. |
| 52 | |
| 53 | Note, append works like it would on a normal list. For example: |
| 54 | |
| 55 | flist.append('./*.c', recursive=True) |
| 56 | |
| 57 | would add a list to the list (i.e., flist = [..., ..., [..., ...]] |
| 58 | |
| 59 | Attempts to add items that aren't in the filesystem raise ValueError: |
| 60 | |
| 61 | >>> test = fslist.FSList() |
| 62 | >>> test[0] = 'This isn't a file.' |
| 63 | Traceback ... |
| 64 | ValueError: Item not found on file system: This isn't a file. |
| 65 | >>> test.append('fslist.py') |
| 66 | >>> test.extend('wx*.py') |
| 67 | >>> print test |
| 68 | ['fslist.py', 'wxEdit.py', 'wxMailitEd.py', 'wxSendIt.py', 'wxtestcode.py'] |
| 69 | >>> |
| 70 | |
| 71 | FSList doesn't keep track of underlying filesystem changes, but you can |
| 72 | test if all the files are still there by testing the instance in a |
| 73 | boolean context. |
| 74 | |
| 75 | >>> import fslist |
| 76 | >>> test = fslist.FSList('*.py') |
| 77 | >>> print test |
| 78 | ['email-unpack.py', 'FileSystemList.py', 'fslist.py', ...] |
| 79 | >>> if test: |
| 80 | print 'Everything's Still there!' |
| 81 | else: |