Package: tl.rename
Overview of tl.renametl.rename provides an implementation of therenameprogram some form of
which is included with many POSIX operating systems. While those tools usually
do only substring or regular expression replacement, planned file name
transformations in tl.rename include:substring replacement as in Gentoo’s rename implementation, for exampleregular expression replacement as seen in Debian’s rename implementationreading the new names from a file or standard inputvarious case transformationsadditional regex replacements inserting formatted countersinteractive renaming using readline, if availableinteractive renaming using an external text editorVersion 0.1 implements reading names from a file or standard input, case
transformations, simple substring replacement and interactive renaming using
readline.The tl.rename package is readily usable as a library but also installs an
executable script that exercises its functionality.Usagerename [options] [file paths]File paths may contain directory paths and be either absolute or relative to
the current working directory. The specified files do not need to exist whenrenameis called but will cause it to fail if they do not exist when the
actual renaming is being done.Options-h,--helpshow this help message and exit-d,--debugdebug mode, do not catch Python exceptions-D,--dry-rundry-run mode, do not touch the file system-sSLICE,--slice=SLICEtransform only a slice of each name, value is
LOWER:UPPER, both bounds are optional-nNAMES_FILE,--names-file=NAMES_FILEread new names from file where - denotes standard input-cCASE,--case=CASEcase-transform names, possible values are upper, lower,
sentence-rFROM_TO,--replace=FROM_TOwhere FROM_TO is two distinct arguments (-r FROM TO);
globally replace first option argument with second,
may be given multiple times-i,--interactiveedit names interactively (assumed if no transformation
is explicitly specified)How it workstl.rename runs one or more string transformations on a sequence of file names
given as command arguments, then renames the files accordingly.Transforming file namesWhich transformations to apply is determined by command line options. If
multiple transformations are selected, they will be run in the following
order:Read names from file (--names-file)Read new file names from a file or standard input, one name per line.Case transformation (--case)Perform a case transformation on the file names:Turn the names into all upper case (upper).Turn the names into all lower case (lower).Apply a style of mixed case, basically capitalizing the first word
of a phrase (sentence). See the doctest examples for sentence
case for the complete rules.Replace substrings (--replace)Replace substrings of the file names where match patterns and replacements
are given literally as two arguments. Any number of replacements can be
made in one go.Interactive replacement (--interactive)Let the user edit each file name in turn using the readline library if
available. This provides comfortable line editing including a history.The list of file names must fulfill the following conditions:Each old and new name is non-empty and contains no null characters.The number of names remains the same during each transformation.Names must be unique before and after each transformation. Trailing path
separators don’t make a difference.Renaming filesOnce all transformations have been performed on the file names, those items
whose names have actually changed will be renamed in the file system (except
if dry-run mode is active). Renaming is subject to the following rules:Files and directories to be renamed must exist at this point.If a file name contains subdirectory path elements and one of those has
changed, the item will be moved to the new directory.When moving items between directories, directory hierarchies will be created
as needed and emptied directories are removed. Empty directories themselves
can be moved.Renaming a file to the name of another existing file overwrites that file.Renaming a directory to the name of an existing empty directory overwrites
that directory.Directories cannot be renamed to existing populated directories or
non-directories, nor can non-directories be renamed to existing directories.Renaming an item to the name of another item that is renamed in the same run
does not overwrite that item. In particular, it is possible to interchange
the names of two items immediately.Symbolic links are never followed.SlicingIt is possible to restrict transformations to a specified portion of each file
name. As an example, this can be useful when applying sentence case to
prefixed file names if the prefix should not count as the beginning of a
sentence.Which portion (or slice) of each file name to subject to transformations is
determined by a specification given as the value of the--sliceoption.
The syntax of this value is that of Python’s simple slices: two integer
numbers separated by a colon. The numbers denote the start and stop index of
the slice where counting starts from 0. The stop index is the index of the
first character after the slice. Both start and stop index may be omitted (but
the colon may not); they default to the beginning and the end of the name (0
and a very big number), respectively. Negative indexes are understood to count
from the end of the name.Let’s give some examples, applied to the file name
“05 - An interesting song.ogg” (28 characters):SpecificationSlice:05 - An interesting song.ogg5:100An interesting song.ogg:-405 - An interesting song5:-4An interesting song5:24An interesting songAbout tl.renameAuthor:Thomas Lotze (thomas@thomas-lotze.de,http://thomas-lotze.de/)Online documentation:http://packages.python.org/tl.rename/PyPI page:http://pypi.python.org/pypi/tl.rename/Issue tracker:https://bitbucket.org/tlotze/tl.rename/issues/Source code:https://bitbucket.org/tlotze/tl.rename/Current change log:https://bitbucket.org/tlotze/tl.rename/raw/tip/CHANGES.txt