我知道我们有 os.walk
但我不知道如何创建它。
假设我在 ubuntu linux 机器上有以下文件夹结构:
Maindir (as root called by script)
+- subdir-one
| +-subdir-two
| +-file
| +-another file
| +-subdir-three
| +-file3
| +-file4
| +-subdir-four
| +- file5
| +- file6
+- subdir-two
+- subdir-three
| +-sub-subdir-two
| +-file
| +-another file
| +-subdir-three
| +-file3
| +-file4
| +-subdir-four
| +-file5
| +-file6
+-subdir-four
+-subdir-two
+-file
+-another file
+-subdir-three
+-file3
+-file4
+-subdir-four
+-file5
+-file6
我想将所有文件从子目录移动到第 2 层的子目录,而不是根目录。
以subdir-one为例:将subdir-four中的所有文件移动到subdir-one(本例为file5和file6),将subdir-thre中的所有文件移动到subdir-one(本例为file3和file4)
Subdir-two 没有其他子目录,因此可以被脚本跳过。
Subdir-three:将所有文件从 sub-subdir-two、subdir-three 和 subdir-four 移动到 subdir-three。
我想你明白了。如果文件被覆盖没问题,如果它们具有相同的名称,它们无论如何都是重复的,这是运行此清理脚本的原因之一。
当所有文件都从子目录中移出时,这意味着子目录将是空的,所以我也想删除空的子目录。
2012 年 1 月 1 日更新:这是 jcollado 提供的更改后的代码,但仍然无法正常工作。顺便说一句,我忘了说我还需要过滤一些目录名。当在目录树中找到这些目录名称时,需要将其排除在处理之外。
我稍微改动了一下代码:
import os, sys
def main():
try:
main_dir = sys.argv[1]
print main_dir
# Get a list of all subdirectories of main_dir
subdirs = filter(os.path.isdir,
[os.path.join(main_dir, path)
for path in os.listdir(main_dir)])
print subdirs
# For all subdirectories,
# collect all files and all subdirectories recursively
for subdir in subdirs:
files_to_move = []
subdirs_to_remove = []
for dirpath, dirnames, filenames in os.walk(subdir):
files_to_move.extend([os.path.join(dirpath, filename)
for filename in filenames])
subdirs_to_remove.extend([os.path.join(dirpath, dirname)
for dirname in dirnames])
# To move files, just rename them replacing the original directory
# with the target directory (subdir in this case)
print files_to_move
print subdirs_to_remove
for filename in files_to_move:
source = filename
destination = os.path.join(subdir, os.path.basename(filename))
print 'Destination ='+destination
if source != destination:
os.rename(source, destination)
else:
print 'Rename cancelled, source and destination were the same'
# Reverse subdirectories order to remove them
# starting from the lower level in the tree hierarchy
subdirs_to_remove.reverse()
# Remove subdirectories
for dirname in subdirs_to_remove:
#os.rmdir(dirname)
print dirname
except ValueError:
print 'Please supply the path name on the command line'
if __name__ == '__main__':
main()
最佳答案
我想如下:
import os
main_dir = 'main'
# Get a list of all subdirectories of main_dir
subdirs = filter(os.path.isdir,
[os.path.join(main_dir, path)
for path in os.listdir(main_dir)])
# For all subdirectories,
# collect all files and all subdirectories recursively
for subdir in subdirs:
files_to_move = []
subdirs_to_remove = []
for dirpath, dirnames, filenames in os.walk(subdir):
files_to_move.extend([os.path.join(dirpath, filename)
for filename in filenames])
subdirs_to_remove.extend([os.path.join(dirpath, dirname)
for dirname in dirnames])
# To move files, just rename them replacing the original directory
# with the target directory (subdir in this case)
for filename in files_to_move:
source = filename
destination = os.path.join(subdir, os.path.basename(filename))
os.rename(source, destination)
# Reverse subdirectories order to remove them
# starting from the lower level in the tree hierarchy
subdirs_to_remove.reverse()
# Remove subdirectories
for dirname in subdirs_to_remove:
os.rmdir(dirname)
注意:您可以将它变成一个函数,只需使用 main_dir
作为参数。
关于python,将所有文件从目录树的第 3、4、5 级移动到第 2 级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8847116/