python,将所有文件从目录树的第3,第4,第5级移动到第2级

时间:2012-01-13 07:13:59

标签: python linux

我知道我们有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-three移动到subdir-one(在本例中为file3和file4)< / p>

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 个答案:

答案 0 :(得分: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作为参数将其转换为函数。