最佳答案
我在删除空目录时有问题。这是我的代码:
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'
我哪里错了?