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

时间:2021-11-27 15:22:12

I know we have os.walk but I can't figure out how to create this.

我知道我们有操作系统。走路,但我想不出怎么做。

Lets say I have the following folder structure on a ubuntu linux box:

假设我有以下文件夹结构在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

I want to move all files from the subdir's to the subdirs on level 2, not to the root level.

我想将所有文件从subdir中移到第2级的subdirs中,而不是根级。

Take subdir-one as example: Move all files in subdir-four to subdir-one (in this case file5 and file6), Move all files from subdir-three to subdir-one (in this case file3 and file4)

以subdir1为例:将subdir- 4中的所有文件移动到subdir- 1(在本例中为file5和file6),将所有文件从subdir- 3移动到subdir- 1(在本例中为file3和file4)

Subdir-two has no other subdirs so can be skipped by the script.

subdir2没有其他subdirs,因此可以被脚本跳过。

Subdir-three: move all files from sub-subdir-two, subdir-three and subdir-four to subdir-three.

subdir3:将所有文件从subsubsubsubsubsubsub -two、subdir3和subdir4移动到subsubsub -three。

I think you get the point. No problem if files are overwritten, if they have the same name they are duplicates anyway, one reason for running this cleanup script.

我想你明白我的意思了。如果文件被覆盖,如果它们有相同的名称,那么它们就是重复的,这是运行这个清理脚本的一个原因。

When all files are moved from the subdir's it means the subdir's will be empty so I also want to remove the empty sub-dirs.

当所有文件从子目录移动时,这意味着子目录将是空的,所以我也想删除空的子目录。

Update on 14-1-2012: This is the changed code given from jcollado but still not working. Btw I forgot to mention that I also need to filter some directory names. These directory names need to be excluded from being processed when found within the directory tree..

更新在14-1-2012:这是由jcollado提供的修改后的代码,但仍然没有工作。顺便说一下,我忘了说,我还需要过滤一些目录名。当在目录树中找到这些目录名时,需要排除这些目录名。

The code I slightly changed:

我修改的代码:

    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 个解决方案

#1


2  

I'd something as follows:

我的东西如下:

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)

Note: You can turn this into a function just using main_dir as a parameter.

注意:您可以使用main_dir作为参数将其转换为函数。

#1


2  

I'd something as follows:

我的东西如下:

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)

Note: You can turn this into a function just using main_dir as a parameter.

注意:您可以使用main_dir作为参数将其转换为函数。