我有两个实用程序函数,可以将模式附加到目录中的一组项目上或将其删除。除了重命名行之外,功能完全相同,这让我相信我可以将它们合并到一个函数中。
以下是两个功能:
def append_items(source, pattern, dirs = True, recurse = False):
"""
Append the pattern to all items within a directory
source = act on this directory
pattern = add this to the start of the file
dirs = apply to directorys
recurse = work recursively
"""
for item in os.listdir(source):
path = os.path.join(source, item)
if "svn" not in item:
if os.path.isdir(path):
# Recurse first
if recurse:
append_items(path, pattern, dirs, recurse)
if dirs:
rename(path, path + pattern)
elif os.path.isfile(path):
name, ext = os.path.splitext(item)
# Append pattern, add extension back
new_path = os.path.join(source, "%s%s" % (name, pattern) + ext)
rename(path, new_path)
#----------------------------------------------------------------------------------------------------------
def remove_string_from_items(source, pattern, dirs = True, recurse = False):
"""
Remove a pattern from all items within a directory
source = directory
pattern = text to replace
"""
for item in os.listdir(source):
path = os.path.join(source, item)
if "svn" not in item:
if os.path.isdir(path):
# Recurse first
if recurse:
remove_string_from_items(path, pattern, dirs, recurse)
if dirs and pattern in item:
target = os.path.join(source, string.replace(item, pattern, ""))
rename(path, target)
elif os.path.isfile(path) and pattern in item:
target = os.path.join(source, string.replace(item, pattern, ""))
rename(path, target)
有人能指出我更清洁的解决方案吗?
答案 0 :(得分:1)
以下内容应与您所拥有的相同,重复代码较少:
def _append_or_remove(source, pattern, dirs = True, recurse = False, append = True):
for item in os.listdir(source):
path = os.path.join(source, item)
if "svn" not in item:
if os.path.isdir(path):
# Recurse first
if recurse:
if append:
append_items(path, pattern, dirs, recurse)
else:
remove_string_from_items(path, pattern, dirs, recurse)
if dirs and pattern in item:
target = os.path.join(source, string.replace(item, pattern, ""))
rename(path, target)
elif os.path.isfile(path) and pattern in item:
if append:
name, ext = os.path.splitext(item)
# Append pattern, add extension back
target = os.path.join(source, "%s%s" % (name, pattern) + ext)
else:
target = os.path.join(source, string.replace(item, pattern, ""))
rename(path, target)
def append_items(source, pattern, dirs = True, recurse = False):
"""
Append the pattern to all items within a directory
source = act on this directory
pattern = add this to the start of the file
dirs = apply to directorys
recurse = work recursively
"""
return _append_or_remove(source, pattern, dirs, recurse, True)
def remove_string_from_items(source, pattern, dirs = True, recurse = False):
"""
Remove a pattern from all items within a directory
source = directory
pattern = text to replace
"""
return _append_or_remove(source, pattern, dirs, recurse, False)
答案 1 :(得分:0)
作为替代方案,这里有一个版本,它将重复的代码封装到一个函数中,该函数由两个特定的函数参数化,一个用于重命名目录,一个用于文件。因此,在特定的append
/ remove
实现中,必须定义的所有内容都是执行特定逻辑的参数函数,然后传递给_workon_items
。
def append_items(source, pattern, dirs = True, recurse = False):
"""
Append the pattern to all items within a directory
source = act on this directory
pattern = add this to the start of the file
dirs = apply to directorys
recurse = work recursively
"""
def dir_rename(path, pattern, source, item):
rename(path, path + pattern)
def file_rename(path, pattern, source, item):
name, ext = os.path.splitext(item)
# Append pattern, add extension back
new_path = os.path.join(source, "%s%s" % (name, pattern) + ext)
rename(path, new_path)
_workon_items(dir_rename, file_rename, source, pattern, dirs, recurse)
#----------------------------------------------------------------------------------------------------------
def remove_string_from_items(source, pattern, dirs = True, recurse = False):
"""
Remove a pattern from all items within a directory
source = directory
pattern = text to replace
"""
def dir_rename(path, pattern, source, item):
if pattern in item:
target = os.path.join(source, string.replace(item, pattern, ""))
rename(path, target)
def file_rename(path, pattern, source, item):
if pattern in item:
target = os.path.join(source, string.replace(item, pattern, ""))
rename(path, target)
_workon_items(dir_rename, file_rename, source, pattern, dirs, recurse)
#----------------------------------------------------------------------------------------------------------
def _workon_items(dir_rename, file_rename, source, pattern, dirs, recurse):
for item in os.listdir(source):
path = os.path.join(source, item)
if "svn" not in item:
if os.path.isdir(path):
# Recurse first
if recurse:
_workon_items(dir_rename, file_rename, path, pattern, dirs, recurse)
if dirs:
dir_rename(path, pattern, source, item)
elif os.path.isfile(path):
file_rename(path, pattern, source, item)