空のディレクトリの削除に問題があります。これが私のコードです:
for dirpath, dirnames, filenames in os.walk(dir_to_search):
//other codes
try:
os.rmdir(dirpath)
except OSError as ex:
print(ex)
引数dir_to_searchは、作業を行う必要があるディレクトリを渡す場所です。そのディレクトリは次のようになります。
test/20/...
test/22/...
test/25/...
test/26/...
上記のフォルダはすべて空であることに注意してください。私は、フォルダにこのスクリプトを実行すると20、25一人で削除されます!しかし、フォルダ25とは26、彼らが空のフォルダであっても、削除されません。
編集:
私が得ている例外は次のとおりです:
[Errno 39] Directory not empty: '/home/python-user/shell-scripts/s3logs/test'
[Errno 39] Directory not empty: '/home/python-user/shell-scripts/s3logs/test/2012'
[Errno 39] Directory not empty: '/home/python-user/shell-scripts/s3logs/test/2012/10'
[Errno 39] Directory not empty: '/home/python-user/shell-scripts/s3logs/test/2012/10/29'
[Errno 39] Directory not empty: '/home/python-user/shell-scripts/s3logs/test/2012/10/29/tmp'
[Errno 39] Directory not empty: '/home/python-user/shell-scripts/s3logs/test/2012/10/28'
[Errno 39] Directory not empty: '/home/python-user/shell-scripts/s3logs/test/2012/10/28/tmp'
[Errno 39] Directory not empty: '/home/python-user/shell-scripts/s3logs/test/2012/10/26'
[Errno 39] Directory not empty: '/home/python-user/shell-scripts/s3logs/test/2012/10/25'
[Errno 39] Directory not empty: '/home/python-user/shell-scripts/s3logs/test/2012/10/27'
[Errno 39] Directory not empty: '/home/python-user/shell-scripts/s3logs/test/2012/10/27/tmp'
どこでミスをしていますか?
rmdir /path/to/25th/folderは、ディレクトリ全体を削除しようとしました。つまり、そのディレクトリは空のディレクトリです。