Next / Previous / Index / ITC Help System / Publications / Site map / NM Tech homepage

How the Python pigfiles script works

Tech Computer Center logo

See the Python source file, pigfiles, for the actual Python code. Here is a general description of the program's operation:

  1. The script starts by importing the sys and os modules from the standard Python library, containing many useful systems programming functions.
  2. We also import our pathinfo module so we can use our PathInfo object (see ` A Python file info object') to represent information about files.
  3. We then define a new class called Pig, which is basically a slightly modified PathInfo object. It replaces that object's built-in __cmp__ method with a new method that causes Pig 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.
  4. 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 Pig object for that pathname. The PathInfo objects are stored in a global list named files.
  5. The files list is sorted using the list type's built-in .sort() method. This orders the Pig objects from largest to smallest file size.
  6. A for loop prints each Pig object in the files list.

Next: oldfiles: Where are my oldest files?
See also: pigfiles: Where are my biggest files?
Previous: How to run the Python pigfiles script
Site map
Index: Keyword index to help pages
Help: New Mexico Tech Information Technology and Communications: Help System
ITC Publications
To report a problem: File a ticket
Send mail to the User consultant on duty or call them at 575-835-5437
Home: About New Mexico Tech

John Shipman,
Last updated: 2004/03/07 01:42:12 UT
QR two-dimensional bar code