python, переместите все файлы с 3-го, 4-го, 5-го на 2-й уровень дерева каталогов - PullRequest
2 голосов
/ 13 января 2012

Я знаю, что у нас есть 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-4 в subdir-one (в данном случае file5 и file6), переместить все файлы из subdir-three в subdir-one (в данном случае file3 и file4)

Subdir-two не имеет других подкаталогов, поэтому может быть пропущен сценарием.

Subdir-three: переместить все файлы из sub-subdir-two, subdir-three и subdir-four в subdir-three.

Я думаю, вы поняли. Нет проблем, если файлы перезаписаны, если они имеют одинаковые имена, они в любом случае являются дубликатами, одна из причин запуска этого сценария очистки.

Когда все файлы перемещаются из подкаталога, это означает, что подкаталоги будут пустыми, поэтому я также хочу удалить пустые подкаталоги.

Обновление от 14-1-2012: это измененный код, полученный от 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 голосов
/ 13 января 2012

Я бы сказал следующее:

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 в качестве параметра.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...