I had one of those "there has to be a better way to do this" moments, so I thought that I might ask here.
I am writing my own version of a backup script that will make a mud backup the files in the ./lib/ directory on a recurring regular basis. I want to having it make it take the ./lib/ directory and duplicate it into a directory ./archives/<datetime>/
I currently use some backwards regex pattern matching to take the current directory portion from the original top path, ie /usr/games/NakedMud/lib/ so that for example /usr/games/NakedMud/lib/pymodules/ extracts to pymodules/
There really has to be a better way than regular expressions to do that; it seems to me like there should be string functions to handle that kind of comparison/extraction; but I can't say that I know python string functions too well and finding a specific thing in the python documentation can be sketchy, anyone have any ideas?
Have you looked at the shutil module in the standard library? Specifically,
shutil.copytree(src, dst[, symlinks=False[, ignore=None]]) Recursively copy an entire directory tree rooted at src. The destination directory, named by dst, must not already exist; it will be created as well as missing parent directories. Permissions and times of directories are copied with copystat(), individual files are copied using copy2().
If symlinks is true, symbolic links in the source tree are represented as symbolic links in the new tree; if false or omitted, the contents of the linked files are copied to the new tree.
If ignore is given, it must be a callable that will receive as its arguments the directory being visited by copytree(), and a list of its contents, as returned by os.listdir(). Since copytree() is called recursively, the ignore callable will be called once for each directory that is copied. The callable must return a sequence of directory and file names relative to the current directory (i.e. a subset of the items in its second argument); these names will then be ignored in the copy process. ignore_patterns() can be used to create such a callable that ignores names based on glob-style patterns.
If exception(s) occur, an Error is raised with a list of reasons.
The source code for this should be considered an example rather than the ultimate tool.