Next
/
Previous
/ Index
/ TCC Help System
/ Publications
/ Site map
/ NM Tech homepage
How the Python oldfiles script works
|
|
See the Python source file,
oldfiles, for the actual Python code. Here
is a general description of the program's operation:
- The script starts by importing the sys and os
modules from the standard Python library, containing many useful
systems programming functions.
- We also import our pathinfo module so we can use our
PathInfo object (see
`pathinfo.py: A Python file info object') to represent information about files.
- We then define a new class called Old, which is
basically a slightly modified PathInfo object. It
replaces that object's built-in __cmp__ method with
a new method that causes Old objects to sort so that
ones for the largest files sort first. We also replace the
__str__ method, to change the way that these objects
are output by a print statement.
- In the main program sequence, the Python function
os.walk() is used to visit every
directory and file. The function Visitor() is called
by os.walk() once for each pathname in the directory
tree, and it creates a Old object for that pathname.
Each Old object is appended to a global list
named files.
- The files list is sorted using the list type's built-in
.sort() method. This orders the Old objects from largest
to smallest file size.
- A for loop prints each Old object in
the files list.
See also: oldfiles: Where are my oldest files?
Previous: How to run the Python oldfiles script
Site map
Index:
Keyword index
to help pages
Help:
New Mexico Tech Computer Center: Help System
TCC Publications
Home:
About New Mexico Tech
John Shipman, john@nmt.edu
Last updated: 2004/03/07 01:41:15 UT
URL: http://www.nmt.edu/tcc/help/lang/python/oldnarr.html