我想将文件从复杂的目录结构移动到一个地方。例如,我有这个深层次的层次结构:
foo/
foo2/
1.jpg
2.jpg
...
我希望它变成:1.jpg
2.jpg
...
我的当前解决方案:
def move(destination):
for_removal = os.path.join(destination, '\\')
is_in_parent = lambda x: x.find(for_removal) > -1
with directory(destination):
files_to_move = filter(is_in_parent,
glob_recursive(path='.'))
for file in files_to_move:
shutil.move(file, destination)
定义:directory
和glob_recursive
。请注意,我的代码仅将文件移动到它们的共同父目录,而不是任意目标。
如何简洁优雅地将复杂层次结构中的所有文件移动到一个单一位置?