Python 技术篇-文件操控:删除本地文件、文件夹。判断文件、文件夹是否存在方法

os.remove() 就是删除文件的
os.removedirs() 就是删除文件夹的
os.path.exists() 用来判断文件或文件夹是否存在

import os

path = "D:\\hello.py"
if(os.path.exists(path)):   # 判断文件是否存在
    os.remove(path)   # 删除文件
    
path = "D:\\hello"
if(os.path.exists(path)):   # 判断文件夹是否存在  
    os.removedirs(path)   # 删除文件夹

喜欢的点个赞❤吧!

posted on 2019-05-08 09:26  小蓝枣  阅读(47)  评论(0编辑  收藏  举报