我在删除空目录时遇到问题。这是我的代码:
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'
我在哪里犯错?