The Dir class

class Dir

Bases: fs.Entry

compress(apply_to_contents=True, callback=None)

Flag this directory so that any new files are automatically compressed. If apply_to_contents is True, iterate over all subdirectories and their files, compressing likewise.

Parameters:
  • apply_to_contents – whether to compress all existing subdirectories and their files
  • callback – called for each subdirectory / file compressed
Returns:

this directory

copy(target_filepath, callback=None, callback_data=None)

Copy this directory to another, which must be a directory if it exists. If it does exist, this directory’s contents will be copied inside it; if it does not exist, this directory will become it. NB To copy this directory inside another, set the target_filepath to other_directory + self.name.

Parameters:
Returns:

a Dir object representing target_filepath

create(security_descriptor=None)

Create this directory, optionally specifying a security descriptor. If the directory already exists, silently succeed. All intervening directories are automatically created if they do not already exist. If any exists but is a file rather than a directory, an exception is raised.

Parameters:security_descriptor – anything accepted by security.security()
Returns:a Dir representing the newly-created directory
delete(recursive=False)

Delete this directory, optionally including its children.

Parameters:recursive – whether to remove all subdirectories and files first
Returns:this Dir
dir(name)

Return a Dir object representing a Directory called name inside this directory.

dirs(pattern=u'*', *args, **kwargs)

Iterate over all directories in this directory which match pattern, yielding a Dir object for each one. Implemented via Dir.entries().

Parameters:pattern – a |-separated list of wildcards to match
disable_encryption()
dismount()

Dismount whatever volume is mounted at this directory

Returns:this Dir
enable_encryption()
encrypt(apply_to_contents=True)
entries(pattern='*', *args, **kwargs)

Iterate over all entries – files & directories – in this directory. Implemented via files()

Parameters:pattern – a |-separated list of wildcards to match
file(name)

Return a File object representing a file called name inside this directory.

files(pattern=u'*', *args, **kwargs)

Iterate over all files in this directory which match pattern, yielding a File object for each one. Implemented via Dir.entries().

Parameters:pattern – a |-separated list of wildcards to match
flat(pattern='*', includedirs=False, depthfirst=False, error_handler=None)

Iterate over this directory and all its subdirectories, yielding one File object on each iteration, and optionally Dir objects as well.

Parameters:pattern – limit the files returned by filename
Includedirs :whether to yield directories as well as files [False]
Depthfirst :as for Dir.walk()
Error_handler :as for Dir.walk()
is_empty()

Returns True if this directory is empty, False otherwise. Will fail if the directory does not yet exist.

mkdir(dirname, security_descriptor=None)

Create :dirname: as a subdirectory of this directory, specifying a security descriptor. This is implemented in terms of create() by concatenating this directory and dirname and calling .create on the resulting Dir object.

Parameters:
Returns:

a Dir representing the newly-created directory

mount(vol)

Mount a volume on this directory. The directory must be empty or an exception is raised. eg:

from winsys import fs
fs.dir ("c:/temp").mkdir ("c_drive").mount ("c:")
Parameters:vol – anything accepted by volume()
Returns:this Dir
mounted_by()

Return the volume mounted on this directory, or None.

Returns:a Volume object or None
rmdir(recursive=False)

Delete this directory, optionally including its children.

Parameters:recursive – whether to remove all subdirectories and files first
Returns:this Dir
uncompress(apply_to_contents=True, callback=None)

Flag this directory so that any new files are automatically not compressed. If apply_to_contents is True, iterate over all subdirectories and their files, uncompressing likewise.

Parameters:
  • apply_to_contents – whether to uncompress all existing subdirectories and their files
  • callback – called for each subdirectory / file uncompressed
Returns:

this directory

unencrypt(apply_to_contents=True)
walk(depthfirst=False, error_handler=None)

Mimic os.walk, iterating over each directory and the files within in. Each iteration yields:

Dir, (generator for Dir objects), (generator for File objects)
Parameters:
  • depthfirst – Whether to use breadth-first (the default) or depth-first traversal
  • error_handler – Whether to continue traversing in the face of access-denied errors
watch(*args, **kwargs)

Return a directory watcher, as per watch()

zip(zip_filename=<Unset>, mode='w', compression=8)

Zip the directory up into a zip file. By default, the file will have the name of the directory with ”.zip” appended and will be a sibling of the directory. Also by default a new zipfile will be created, overwriting any existing one, and standard compression will be used. Filenames are stored as relative to this dir.

A different zip filename can be specific as the zip_filename parameter, and this can be appended to (if it exists) by specifying “a” as the mode param.

The created / appended zip file is returned.

Parameters:
  • zip_filename – The name of the zip file to hold the archive of this directory and its children. [directory.zip]
  • mode – cf zipfile.ZipFile
  • compressions – cf zipfile.ZipFile
Returns:

a File object representing the resulting zip file

Previous topic

The File class

Next topic

fs Constants

This Page