python,将所有文件从第三、第四、第五层移动到第二层目录树
我知道我们有一个叫做 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
我想把所有子文件夹里的文件移动到第二层的子文件夹里,而不是移动到根目录。
以 subdir-one 为例:把 subdir-four 里的所有文件(在这个例子中是 file5 和 file6)移动到 subdir-one;再把 subdir-three 里的所有文件(在这个例子中是 file3 和 file4)也移动到 subdir-one。
subdir-two 没有其他子文件夹,所以可以在脚本中跳过它。
对于 subdir-three:把 sub-subdir-two、subdir-three 和 subdir-four 里的所有文件都移动到 subdir-three。
我想你明白我的意思了。如果文件名相同,覆盖也没关系,反正它们是重复的,这也是我想运行这个清理脚本的原因之一。
当所有文件都从子文件夹中移动后,这些子文件夹就会变空,所以我也想把这些空的子文件夹删除掉。
更新于 2012 年 1 月 14 日:这是 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()
1 个回答
2
我想要的东西如下:
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
作为参数传入就可以了。