使用python怎么删除任意一个路径下的文件夹

707
2021/3/8 14:01:46
栏目: 编程语言
开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

使用python怎么删除任意一个路径下的文件夹

在python中使用shutil模块删除指定路径下的文件夹,具体方法如下:

import os

import shutil #导入shutil模块

delList = []

delDir = "/home/test"

delList = os.listdir(delDir )

for f in delList:

filePath = os.path.join( delDir, f )

if os.path.isfile(filePath):

os.remove(filePath)

print filePath + " was removed!"

elif os.path.isdir(filePath):

shutil.rmtree(filePath,True)

print "Directory: " + filePath +" was removed!"

辰迅云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

推荐阅读: qt怎么调用python脚本