The FilePath class

class FilePath

Bases: unicode

A unicode subtype which knows about path structures on Windows. The path itself need not exist on any filesystem, but it has to match the rules which would make it possible.

FilePaths can be absolute or relative. The only difference is that the root attribute is empty for relative paths. They can be added to each other or to other unicode strings which will use os.path.join semantics.

A FilePath offers quick access to the different parts of the path:

  • parts - a list of the components (cf fs.get_parts())
  • root - the drive or UNC server/share ending in a backslash unless a drive-relative path
  • filename - final component
  • name - same as filename (convenience)
  • dirname - all path components before the last
  • path - combination of root and dirname
  • parent - combination of root and all path components before second penultimate; raises an exception is FilePath is relative.
  • base - base part of filename (ie the piece before the dot)
  • ext - ext part of filename (ie the dot and the piece after)
Path root filename name dirname path parent base ext
\\a\b\c\d.txt \\a\b\ d.txt d.txt c \\a\b\c \\a\b\c d .txt
c:\boot.ini c:\ boot.ini boot.ini _ c:\ c:\ boot .ini
boot.ini _ boot.ini boot.ini _ _ x_fs boot .ini
c:\t c:\ t t _ c:\ c:\ t _
c:\t\ c:\ t t _ c:\ c:\ t _
c:\t\a.txt c:\ a.txt a.txt t c:\t c:\t a .txt
c:a.txt c: a.txt a.txt _ c: x_fs a .txt
a.txt _ a.txt a.txt _ _ x_fs a .txt
absolute()

Return an absolute version of the current FilePath, whether relative or not. Use os.path.abspath() semantics.

abspath()

Return an absolute version of the current FilePath, whether relative or not. Use os.path.abspath() semantics.

changed(root=None, dirname=None, filename=None, base=None, infix=None, ext=None)

Return a new FilePath with one or more parts changed. This is particularly convenient for, say, changing the extension of a file or producing a version on another path, eg:

from winsys import fs, shell

BACKUP_DRIVE = "D:\"
for f in fs.flat (shell.special_folder ("personal"), "*.doc"):
  f.copy (f.changed (root=BACKUP_DRIVE))
classmethod factory(filepath)

Designed to be redefined in a subclass so that the __add__() and __radd__() methods can return the appropriate type.

classmethod from_parts(root, dirname, base, ext)

Recreate a filepath from its constituent parts. No real validation is done; it is assumed that the parameters are valid parts of a filepath.

is_relative()

A filepath is relative if it has no root or if its root is a drive letter only (without a directory backslash).

relative_to(other)

Return this filepath as relative to another. cf utils.relative_to()

Previous topic

The Drive class

Next topic

The Entry class

This Page