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)   # 刪除文件夾

喜歡的點個贊❤吧!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章