Next / Previous / Contents / TCC Help System / NM Tech homepage

Abstract

Describes a class in the Python programming language that represents information about a file, as well as several useful scripts that use that class.

This publication is available in Web form and also as a PDF document. Please forward any comments to tcc-doc@nmt.edu.

Table of Contents

1. Introduction: Why PathInfo?
2. The interface to the PathInfo object
3. bigfiles.py: Where are my biggest files?
4. oldfiles.py: Sorting your files by modification date
5. softlinks.py: Find soft links in a directory tree
6. Source code for PathInfo
6.1. Prologue
6.2. Module constants
6.3. class PathInfo
6.4. PathInfo.__init__(): The class constructor
6.5. PathInfo.isFile(): Is this an ordinary file?
6.6. PathInfo.isDir(): Is this a directory?
6.7. PathInfo.isLink(): Is this a soft link?
6.8. PathInfo.absPath(): Absolute path
6.9. PathInfo.realPath(): Actual absolute path
6.10. PathInfo.ownerCanRead(), etc: Predicates for permission testing
6.11. PathInfo.modTime(): Modification timestamp in human units
6.12. PathInfo.__str__(): Convert to a string
6.13. PathInfo.__fileType(): Get the file type code
6.14. PathInfo.__permFlags(): Format all the permissions
6.15. PathInfo.__rwx(): Format three permission bits
6.16. PathInfo.__dasher(): Format a permission bit
6.17. PathInfo.__cmp__(): Define the comparison operator on PathInfo objects
7. Source code for bigfiles.py
7.1. bigfiles.py: Code prologue
7.2. bigfiles.py: The main program
7.3. report(): Generate one directory tree's report
7.4. class BigInfo: The PathInfo subclass
7.5. BigInfo.__init__(): Constructor
7.6. BigInfo.__cmp__(): The comparator method
7.7. BigInfo.__str__(): String conversion method
7.8. class BigReport: The class for the whole application
7.9. BigReport.__init__(): Constructor
7.10. BigReport.__visitor(): Visitor function for os.path.walk()
7.11. BigReport.genFiles(): Generate the report
7.12. Epilogue
8. Source code for oldfiles.py
8.1. oldfiles.py: Code prologue
8.2. oldfiles.py: The main program
8.3. report(): Generate one directory tree's report
8.4. class OldInfo: The PathInfo subclass
8.5. OldInfo.__init__()
8.6. OldInfo.__cmp__(): The comparator method
8.7. OldInfo.__str__(): String conversion method
8.8. class OldReport: The application class
8.9. OldReport.__init__(): Constructor
8.10. OldReport.__visitor(): Visitor function for os.path.walk()
8.11. OldReport.genFiles(): Generate the report
8.12. Epilogue
9. Source code for softlinks.py
9.1. softlinks.py: Code prologue
9.2. main(): Main program
9.3. report(): Generate one tree's report
9.4. class LinkReport: Link report container
9.5. LinkReport.__init__(): Constructor
9.6. LinkReport.__visitor(): Visitor function for os.path.walk()
9.7. LinkReport.genLinks(): Generate links
9.8. class LinkInfo: The PathInfo subclass
9.9. LinkInfo.__init__(): Constructor
9.10. LinkInfo.__str__(): Convert to a string
9.11. Epilogue

1. Introduction: Why PathInfo?

This document describes a small Python class called PathInfo. This class was written for two reasons:

  • Each instance of a PathInfo object represents a snapshot of the state of a file, directory, or other such object, with a number of methods to help you look at aspects of the file such as its size and permissions.

  • It is a good example of a small Python class for use in introducing object-oriented programming to new Python programmers.

Also, we present some small scripts that use the PathInfo object for practical, everyday tasks:

  • When you have a lot of disk space tied up and want to find some large files you can offload or delete, you want to see a list of your largest files in descending order by size. See Section 3, “bigfiles.py: Where are my biggest files?”.

  • Sometimes you want to look at files according to when they were last modified. For example, you might want to find the files you've used least recently to help you weed out unused files. Or you might be trying to find some files inside a large file structure, and you don't know where they are, but you do remember when you were working on them. See Section 4, “oldfiles.py: Sorting your files by modification date”.

This project exhibits a number of useful software technologies. In addition to object-oriented programming:

Source files contained in this package include:

  • pathinfo.py: The module containing the PathInfo class.

  • bigfiles.py: The script for finding large files.

  • oldfiles.py: The script for sorting files by modification time.