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
PathInfo?PathInfo objectbigfiles.py: Where are my biggest files?oldfiles.py: Sorting your files by modification datesoftlinks.py: Find soft links in a directory treePathInfoclass PathInfoPathInfo.__init__(): The
class constructorPathInfo.isFile(): Is this
an ordinary file?PathInfo.isDir(): Is this
a directory?PathInfo.isLink(): Is this
a soft link?PathInfo.absPath(): Absolute
pathPathInfo.realPath(): Actual absolute
pathPathInfo.ownerCanRead(),
etc: Predicates for permission testingPathInfo.modTime():
Modification timestamp in human unitsPathInfo.__str__(): Convert
to a stringPathInfo.__fileType(): Get
the file type codePathInfo.__permFlags():
Format all the permissionsPathInfo.__rwx(): Format
three permission bitsPathInfo.__dasher(): Format
a permission bitPathInfo.__cmp__():
Define the comparison operator on PathInfo objectsbigfiles.pybigfiles.py: Code prologuebigfiles.py: The main programreport(): Generate one
directory tree's reportclass BigInfo: The PathInfo
subclassBigInfo.__init__(): ConstructorBigInfo.__cmp__():
The comparator methodBigInfo.__str__(): String
conversion methodclass BigReport:
The class for the whole applicationBigReport.__init__():
ConstructorBigReport.__visitor():
Visitor function for os.path.walk()BigReport.genFiles():
Generate the reportoldfiles.pyoldfiles.py: Code prologueoldfiles.py: The main programreport(): Generate one
directory tree's reportclass OldInfo: The PathInfo
subclassOldInfo.__init__()OldInfo.__cmp__(): The
comparator methodOldInfo.__str__(): String
conversion methodclass OldReport: The
application classOldReport.__init__():
ConstructorOldReport.__visitor():
Visitor function for os.path.walk()OldReport.genFiles():
Generate the reportsoftlinks.pysoftlinks.py: Code prologuemain(): Main programreport(): Generate one tree's
reportclass LinkReport: Link
report containerLinkReport.__init__():
ConstructorLinkReport.__visitor(): Visitor
function for os.path.walk()LinkReport.genLinks(): Generate
linksclass LinkInfo: The PathInfo subclassLinkInfo.__init__(): ConstructorLinkInfo.__str__(): Convert to a
string
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:
This document includes all the Python source code for
the PathInfo object and the example scripts that use it.
For more on this practice, called literate
programming, see A source extractor for lightweight
literate programming.
The code was developed using the Cleanroom or zero-defect style; for the author's practice and conventions, see The cleanroom software development methodology.
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.