最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】Python中如何递归的删除整个非空文件夹

Python crifan 3270浏览 0评论

折腾:

【记录】尝试用Python操作PhantomJS+Selenium去模拟购物操作

期间,根据配置中参数isResetOutput为true时,则每次运行之前,都删除掉output文件夹,及其中的文件,图片

python delete folder recursive

directory – Deleting the folders in python recursively – Stack Overflow

file – How do I remove/delete a folder that is not empty with Python? – Stack Overflow

python – How to delete a file or folder? – Stack Overflow

【总结】

最后用:

<code>import shutil

# init output folder
if gCfg["isResetOutput"] :
    if os.path.exists(gCfg["outputFolder"]):
        shutil.rmtree(gCfg["outputFolder"])
</code>

即可。

注意:

删除之前要先用os.path.exists判断是非存在该目录

如果不存在就去删除,则会报错:OSError: [Errno 2] No such file or directory

转载请注明:在路上 » 【已解决】Python中如何递归的删除整个非空文件夹

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
82 queries in 0.149 seconds, using 21.99MB memory