Top

grizzled.file module

This module contains file- and path-related methods, classes, and modules.

Functions

def copy(

files, target_dir, create_target=False)

Copy one or more files to a target directory.

Parameters

  • files (str or list of str): a string representing a single path, or a list of strings representing multiple paths, to be copied
  • target_dir (str): path to the target directory
  • create_target (bool): whether or not to create the target

Returns Nothing

Raises

  • OSError: target_dir does not exist and create_target is False.

def eglob(

pattern, directory='.')

Extended glob function that supports the all the wildcards supported by the Python standard glob routine, as well as a special ** wildcard that recursively matches any directory.

Parameters

  • pattern (str): The wildcard pattern.
  • directory (str): The directory in which to do the globbing. Defaults to .

Yields

The matched paths.

def list_recursively(

dir)

Recursively list the contents of a directory. Yields the contents of the directory and all subdirectories. This method returns a generator, so it evaluates its recursive walk lazily. This function is just a simple wrapper around os.walk.

Each yielded value is a partial path, relative to the original directory.

Parameters

  • dir (str): Path to directory to list
  • include_files (bool): Whether or not to yield directories. True by default.
  • include_dirs (bool): Whether or not to yield files. True by default.

Yields

partial paths of all directories and/or files below the specified directory

Raises

ValueError: If dir does not exist, or if dir exists but is not a directory.

def native_path(

path)

Converts a path name from universal path notation to the operating system-specific format. Universal path notation always uses a Unix-style "/" to separate path elements. A native path can be converted to a universal path via the universal_path() function. Note that on POSIX-compliant systems, this function simply returns the path parameter unmodified.

Parameters

  • path (str): the universal path to convert to native path notation

Returns

The path in native path notation.

def pathsplit(

path)

Split a path into an array of path components, using the file separator (e.g., '/' on POSIX systems) that's appropriate for the underlying operating system. Does not take drive letters into account. If there's a Windows drive letter in the path, it'll end up with the first component.

Parameters

  • path (str): path to split. Can be relative or absolute

Returns

a list of path components

def recursively_remove(

dir)

Recursively remove all files and directories below and including a specified directory.

Parameters

  • dir (str): path to directory to remove

def touch(

files)

Similar to the Unix touch command, this function:

  • updates the access and modification times for any existing files in a list of files
  • creates any non-existent files in the list of files

files can be a single string or a sequence of strings.

If any file in the list is a directory, this function will throw an exception.

  • If ns is not None, it must be a 2-tuple of the form (atime_ns, mtime_ns) where each member is an int expressing nanoseconds.
  • If times is not None, it must be a 2-tuple of the form (atime, mtime) where each member is an int or float expressing seconds.
  • If times is None and ns is None, this is equivalent to specifying ns=(atime_ns, mtime_ns) where both times are the current time.
  • If both are specified, ValueError is raised.

def universal_path(

path)

Converts a path name from its operating system-specific format to a universal path notation. Universal path notation always uses a Unix-style "/" to separate path elements. A universal path can be converted to a native (operating system-specific) path via the native_path() function. Note that on POSIX-compliant systems, this function simply returns the path parameter unmodified.

Parameters

  • path (str): the path to convert to universal path notation

Returns

The path in universal path notation.

Like the standard os.unlink() function, this function attempts to delete a file. However, it swallows any exceptions that occur during the unlink operation, making it more suitable for certain uses (e.g., in atexit handlers).

Parameters

  • paths (str or sequence of str): path(s) to unlink

Sub-modules

grizzled.file.includer

Introduction

The grizzled.file.includer module contains a class that can be used to process includes within a text file, returning a file-like object. It also contains some utility functions that permit using include-enabled files in other contexts.

Include Syntax

Th...