image

the sleepy snake

index :: path

path

wrapper moddule for os.path.

Like most others I find the os.path module not very handy. Its too much typing in most cases many frequently required methods are not instantly at hand. So here is my approach for a higher level wrapper, neither complete nor heavily tested.

sample

# it gets boring to type over and over the following term to get the path of some 
# other file in the same dir as the module you're working with
some_other = os.path.join(os.path.basename(__file__), 'someother.file')
# feels better like this...
some_other = path.replace_file(__file__, 'someother.file')

The path module peovides the following methods:

directory

get_dir(path)returns the directory part of a path
replace_dir(path, dirname='')replaces the directory part of a path
remove_dir(path)removes the directory part of a path

drive

add_drive(path, drive)adds a drive to path
get_drive(path)returns the drive part of a path
has_drive(path)checks if the path has a drive part
replace_drive(path, drive='')replaces the drive part of a path
remove_drive(path)removes the drive part of a path

extension

add_ext(path, ext)adds a file extension to path
get_ext(path, components=1)returns the file extension part of a path
has_ext(path, ext="")checks if the path has a file extension part
replace_ext(path, ext='', components=1)replaces the file extension part of a path
remove_ext(path, components=1)removes the file extension part of a path

file

add_file(path, file)adds a file part to a path
get_file(path, stripext=False, components=1)returns the file part of a path
replace_file(path, filename='')replaces the file part of a path
remove_file(path)removes the file part of a path

url

Note: this seection needs rework, the functions may or may not work

add_url(path, url)adds a url part to path
get_url(path)returns the url part of a path
has_url(path)checks if the path has an url part
replace_url(path, url='')replaces the url part of a path
remove_url(path)removes the url part of a path
to_url(path, sep=os.sep)converts separators to slashes

iterators

iter_children(path)Iterator. Yields the next child component in turn
iter_components(path)Iterator. Yields the next component of a path in turn
iter_parents(path)Iterator. Yields the next parent component in nturn.
to_list(path)splits a path into its components

relative paths

is_child(parentpath, childpath, emidiate=False, ignorecase=True)checks if childpath is a child of parentpath
is_samepath(ath1, path2, ignorecase=True)checks if path1 is the same path as path2
html_abspath(pathfrom, pathto)constructs an absolute path from a html style relative path
html_relpath(pathfrom, pathto, ignorecase=True)constructs a html style relative path to pathto
relpath(parentpath, childpath, ignorecase=True)constructs a relative path to parent

attributes

is_readonly(path)checks if a file system object is readonly
set_readonly(path, flag=False)sets or clears the readonly flag for a file system object

taken from os, os.path and shutil

common_prefix(paths)]same as os.path.commonprefix()
copy_file(source, dest)same as shutil.copyfile()
exists(path)]same as os.path.exists()
is_dir(path)same as os.path.isdir()
is_file(path)same as os.path.isfile()
is_link(path)same as os.path.islink()
join(*components)same as os.path.join()
list_dir(directory)same as os.listdir()
make_dir(path, recursive=True)same as os.mkdir(), except for the recursive flag, wich calls os.mkdirs() if set toTrue
normpath(path)same as os.path.normpath()
remove_tree(path, ignore_errors=False, onerror=None)same as shutil.rmtree()
walk(directory)same as os.path.walk()
walk_sorted(directory)same as os.path.walk(), except that directories and files are returned sorted
renamesource, dest)same as os.rename()
remove(path)same as os.remove()
stat(path)same as os.stat()
lstatpath)same as os.lstat()

others

add_slash(path, slash=os.sep)adds a trailing shlash to a path
compact_path(path, w, measure=len, max_pardirs=2)compacts a filepath to fit into a desired width
patch_filename(path, prefix='')constructs one of these unique myfile(1).txt names
import_relative(__file__, what, globals=None, locals=None, names=None)imports a module relative to another
quote(path)quotes a path
quote_spaces(path)quotes a path if it contains spaces
remove_slash(path)removes trailing shlash or backslash from path if becessary
unquote(path)removes quotes from a path

win32 specific

Note: to make these functions work, apart from running a windows os the ctypes module has to be installed to call the relevant system dlls. The ctypes module will be part of the python distribution as soon as 2.5 is shipped out.

drive_list()returns a list containing the drive letters of currently mounted drives
drive_type(drive)returns a description of a drive
drive_info(drive)returns extended information about a drive
get_free_space(drive=None)returns a calculation of thre free space on a drive
to_long_path(path)converts a micros~1 short path to a long path
to_short_path(path)converts a long path to a micros~1 short path