Reputation:
I have this function:
def get_previous_backups(backup_dir, extension):
"""Returns a list of backup files matching the date format and file extension"""
return [os.path.basename(x) for x in
glob.glob(backup_dir + DIR_PATH + host + '.[0-9][0-9][0-9][0-9]' +
'-[0-9][0-9]' + '-[0-9][0-9]' + '.' + extension)]
This function searches in a given directory for files matching the given pattern. Later the results will be used to remove the oldest file. Files like mx01.2015-10-15.tar.bz2 would match the filter. So the function works like a charm, but here is my problem. This function is part of a backup script I wrote. It makes incremental and full backups. The full backups have a slightly different name. In front of the date of the tar file is a _FULL to make it clear that this file contains a complete backup of a system. These files should be removed too. I've tried to use an optional parameter for the function like
def get_previous_backups(backup_dir, extension, full=None):
and using it in the concatenation like:
'-[0-9][0-9]' + '-[0-9][0-9]' + full + '.' + extension)]
or with format
'-[0-9][0-9]' + '-[0-9][0-9]' + {} + '.' + extension)].format(full)
But that didn't work because you can't concatenate dicts or nontype objects with strings. So my question now is:
Is there a possibility to realise this within this one function, or do I have to use a second one just for the full backups?
Upvotes: 2
Views: 93
Reputation: 699
Instead of None
use ""
, so you can concatenate:
def get_previous_backups(backup_dir, extension, full=""):
Upvotes: 1