Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python,将所有文件从目录树的第三、第四、第五级移动到第二级_Python_Linux - Fatal编程技术网

python,将所有文件从目录树的第三、第四、第五级移动到第二级

python,将所有文件从目录树的第三、第四、第五级移动到第二级,python,linux,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 | +-

我知道我们有
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-four中的所有文件移动到subdir-one(在本例中为file3和file4)

Subdir two没有其他Subdir,因此脚本可以跳过它

细分曲面三:将所有文件从细分曲面二、细分曲面三和细分曲面四移动到细分曲面三

我想你明白了。如果文件被覆盖没有问题,如果它们具有相同的名称,它们仍然是重复的,这是运行此清理脚本的一个原因

当所有文件从子目录移动时,这意味着子目录将为空,因此我还想删除空的子目录

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()

我想说以下几点:

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\u dir
作为参数将其转换为函数。

只是一个建议,也许可以尝试将程序分为三个独立的级别。一个是在层次结构中向下移动两个步骤,一个是将下面的所有文件移动到“根”(现在是您在上一步中离开的位置),另一个是递归地擦除所有空dir。它必须在Python中,还是可以使用shell命令<代码>用于*/中的d;找到“$d”-depth-mindepth 2-type f-path'*/*/*/*/*'-exec mv{}“$d”\-o-类型d-空-删除;完成Hi jcollado。您发送给我的代码不完整。它不起作用。仅处理数组中的最后一个目录。我试图更改您的代码,但运气不好。我把修改后的代码放在下面的答案中,但它不起作用…嗨,大卫,谢谢你的回答。我真的很想用python来回答这个问题。shell是可能的,但您的解决方案不是动态的?如果我们有一个10层深的目录树会发生什么??