私はos.walk
を知っていますが、これを作成する方法を理解できません。python、ディレクトリツリーの3番目、4番目、5番目から2番目のレベルにすべてのファイルを移動
は、私は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-one(この場合はfile5とfile6)に移動し、すべてのファイルをsubdir-3からsubdir-oneに移動します(この場合はfile3とfile4)
Subdir-twoには他のサブディレクトリがないため、スクリプトでスキップすることができます。
Subdir-three:すべてのファイルをsub-subdir-2、subdir-three、およびsubdir-fourからsubdir-threeに移動します。
私はあなたがポイントを得ると思います。とにかく重複しているのと同じ名前のファイルが上書きされても問題ありません。このクリーンアップスクリプトを実行する理由の1つです。
すべてのファイルをサブディレクトリから移動すると、サブディレクトリが空であるため、空のサブディレクトリも削除します。
14-1-2012の更新:これはjcolladoから与えられた変更されたコードですが、それでも動作しません。 Btw私はまた、いくつかのディレクトリ名をフィルタリングする必要があることを忘れていました。
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)
注:次のように私は何かをいただきたい
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()
あなたのプログラムを独立した3つのレベルの歩みに分けるようにしてください。階層内の2つのステップを経て、下のすべてのファイルを「ルート」(これは前のステップで残した場所)に移動し、もう1つはすべての空のディレクトリを再帰的に消去するステップです。 –
これはPythonでなければならないのですか、あるいはシェルコマンドを使用できますか? 'のために* /; "$ d"を見つける - 深さ - mindepth 2 - タイプf - パス '*/*/*/*' - 経験mv {} "$ d" \; -o -type d -empty -delete; done' –
こんにちは。あなたが送ったコードは完全ではありません。それは動作していません。配列内の最後のディレクトリだけが処理されます。私は運がないあなたのコードを変更しようとします。私は下の答えで変更されたコードを入れましたが、それは動作していません... – Bob