我在删除空目录时遇到了问题。这是我的代码:
参数
请注意,所有上述文件夹均为空。当我运行此脚本时,仅
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
命令删除整个目录。这意味着该目录为空! - sriram