dkfileutils – file and directory utilities¶
Documentation: http://pythonhosted.org/dkfileutils/
Contains the following modules¶
See the documentation link (above) and module documentation for detailed docs.
changed¶
Code to check if directory contents have changed since last check.
listfiles¶
Yield (digest, fname) tuples for all interesting files in dirname. The file names are relative to curdir unless otherwise specified.
path¶
“Poor man’s pathlib”. Object-oriented wrapper around os.path and friends. Similar to the Python 3 pathlib, however paths are str subclasses and thus much easier to use in an environment where os.path calls are interspersed with object-oriented code.
pfind¶
Find directory where a file is located by walking up parent directories.
which¶
Functions for finding executable files on the path.
Installing from PyPI¶
This is what you want if you just want to use dkfileutils:
pip install dkfileutils
As a source package¶
This is what you want if you are developing dkfileutils or want to make local changes to the source code.
pip install -e <path>
See docs/ folder for documentation.
Developing dkfileutils¶
Note
if you’re using this as a template for new projects, remember to
python setup.py register <projectname>
before you upload to
PyPi.
Uploading to PyPI¶
This requires pyinvoke (pip install invoke
) and dk-tasklib
(pip install dk-tasklib
). These are not listed as requirements.
Create a new version:
inv upversion
Check in new version numbers:
git add ... git commit ...
Verify that everything is copacetic:
inv publish
Tag the release (replace 1.2.3 with the new version number):
git tag -a v1.2.3 -m "Version 1.2.3"
git push origin --tags
then push to PyPi:
inv publish -f
Running tests¶
One of:
python setup.py test
py.test dkfileutils
with coverage (one of):
py.test --cov=.
coverage run runtests.py && coverage report
Building documentation¶
python setup.py build_sphinx
API documentation¶
changed¶
Check if contents of directory has changed.
-
dkfileutils.changed.
changed
(dirname, filename='.md5', args=None, glob=None)[source]¶ Has glob changed in dirname
- Args:
- dirname: directory to measure filename: filename to store checksum
listfiles¶
List interesting files.
-
dkfileutils.listfiles.
list_files
(dirname='.', digest=True)[source]¶ Yield (digest, fname) tuples for all interesting files in dirname.
-
dkfileutils.listfiles.
read_skipfile
(dirname='.', defaults=None)[source]¶ The .skipfile should contain one entry per line, listing files/directories that should be skipped by
list_files()
.
path¶
Poor man’s pathlib.
(Path instances are subclasses of str, so interoperability with existing os.path code is greater than with Python 3’s pathlib.)
-
class
dkfileutils.path.
Path
[source]¶ Poor man’s pathlib.
-
absolute
()¶ Return the absolute version of a path.
-
access
(path, mode) → True if granted, False otherwise[source]¶ Use the real uid/gid to test for access to a path. Note that most operations will use the effective uid/gid, therefore this routine can be used in a suid/sgid environment to test if the invoking user has the specified access to the path. The mode argument can be F_OK to test existence, or the inclusive-OR of R_OK, W_OK, and X_OK.
-
commonprefix
(*args)[source]¶ Given a list of pathnames, returns the longest common leading component
-
expandvars
()[source]¶ Expand shell variables of the forms $var, ${var} and %var%.
Unknown variables are left unchanged.
-
ext
¶
-
glob
(pat)[source]¶ pat can be an extended glob pattern, e.g. ‘**/*.less’ This code handles negations similarly to node.js’ minimatch, i.e. a leading ! will negate the entire pattern.
-
join
(*args)[source]¶ Join two or more pathname components, inserting “” as needed. If any component is an absolute path, all previous path components will be discarded.
-
list
(filterfn=<function <lambda> at 0x044768F0>)[source]¶ Return all direct descendands of directory self for which filterfn returns True.
-
listdir
(path) → list_of_strings[source]¶ Return a list containing the names of the entries in the directory.
path: path of directory to listThe list is in arbitrary order. It does not include the special entries ‘.’ and ‘..’ even if they are present in the directory.
-
makedirs
(path[, mode=0777])[source]¶ Super-mkdir; create a leaf directory and all intermediate ones. Works like mkdir, except that any intermediate path segment (not just the rightmost) will be created if it does not exist. This is recursive.
-
normcase
()[source]¶ Normalize case of pathname.
Makes all characters lowercase and all slashes into backslashes.
-
parent
¶
-
parents
¶
-
removedirs
(path)[source]¶ Super-rmdir; remove a leaf directory and all empty intermediate ones. Works like rmdir except that, if the leaf directory is successfully removed, directories corresponding to rightmost path segments will be pruned away until either the whole path is consumed or an error occurs. Errors during this latter phase are ignored – they generally mean that a directory was not empty.
-
renames
(old, new)[source]¶ Super-rename; create directories as necessary and delete any left empty. Works like rename, except creation of any intermediate directories needed to make the new pathname good is attempted first. After the rename, directories corresponding to rightmost path segments of the old name will be pruned way until either the whole path is consumed or a nonempty directory is found.
Note: this function can fail with the new directory structure made if you lack permissions needed to unlink the leaf directory or file.
-
rmtree
(subdir=None)[source]¶ Recursively delete a directory tree.
If ignore_errors is set, errors are ignored; otherwise, if onerror is set, it is called to handle the error with arguments (func, path, exc_info) where func is os.listdir, os.remove, or os.rmdir; path is the argument to that function that caused it to fail; and exc_info is a tuple returned by sys.exc_info(). If ignore_errors is false and onerror is None, an exception is raised.
-
split
(sep=None, maxsplit=-1)[source]¶ Split a pathname.
Return tuple (head, tail) where tail is everything after the final slash. Either part may be empty.
-
splitdrive
()[source]¶ Split a pathname into drive and path specifiers. Returns a 2-tuple “(drive,path)”; either part may be empty
-
splitext
()[source]¶ Split the extension from a pathname.
Extension is everything from the last dot to the end, ignoring leading dots. Returns “(root, ext)”; ext may be empty.
-
splitunc
()[source]¶ Split a pathname into UNC mount point and relative path specifiers.
Return a 2-tuple (unc, rest); either part may be empty. If unc is not empty, it has the form ‘//host/mount’ (or similar using backslashes). unc+rest is always the input path. Paths containing drive letters never have an UNC part.
-
startfile
(*args, **kw)[source]¶ startfile(filepath [, operation]) - Start a file with its associated application.
When “operation” is not specified or “open”, this acts like double-clicking the file in Explorer, or giving the file name as an argument to the DOS “start” command: the file is opened with whatever application (if any) its extension is associated. When another “operation” is given, it specifies what should be done with the file. A typical operation is “print”.
startfile returns as soon as the associated application is launched. There is no option to wait for the application to close, and no way to retrieve the application’s exit status.
The filepath is relative to the current directory. If you want to use an absolute path, make sure the first character is not a slash (“/”); the underlying Win32 ShellExecute function doesn’t work if it is.
-
touch
(mode=438, exist_ok=True)[source]¶ Create this file with the given access mode, if it doesn’t exist. Based on:
-
pfind¶
CLI usage: pfind path filename
will find the closest ancestor directory
conataining filename (used for finding syncspec.txt and config files).