我是靠谱客的博主 怕黑康乃馨,这篇文章主要介绍shutil.rmtree 函数 OSError: [Errno 39] Directory not empty: 错误原因,现在分享给大家,希望可以做个参考。
这个错误一般出现在 nfs 文件系统中,本人在用 flask 写了一个文件下载程序,在下载完成之后,会有一步删除临时文件,该临时文件所在的文件系统为 nfs,于是就遇到了如上错误。经过查阅资料发现,这是 nfs 系统引起的。在打开一个文件时,nfs 文件系统会在文件所在的目录生成一个 .nfs 文件,如果有文件描述符为关闭,这时去删除文件所在的目录,就会发生如上错误。
我当时调用的是 flask 的 send_file 函数来实现文件下载的。
复制代码
1
2
3
4
5
6try: response = make_response(send_file(os.path.join(tmp_dir,file_name))) response.headers["Content-Disposition"] = "attachment;filename=%s"%file_name return response finally: shutil.rmtree(tmp_dir)
我觉得,在我删除文件时, send_file 函数未释放打开的文件描述符,所以会报如上错误。
可以选择在 shutil.rmtree(os.path.join(tmp_dir),ignore_errors=True) 或
复制代码
1commands.getstatusoutput("rm -rf %s" % tmp_dir) 来忽略错误,但是无论哪种方法,如果文件描述符未关闭,此文件所在的文件夹都是无法删掉的。
参考:
http://stackoverflow.com/questions/11228079/python-remove-directory-error-file-exists
https://github.com/hashdist/hashdist/issues/113
最后
以上就是怕黑康乃馨最近收集整理的关于shutil.rmtree 函数 OSError: [Errno 39] Directory not empty: 错误原因的全部内容,更多相关shutil.rmtree内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复