python

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

养鱼的猫咪
132
2021-03-08 14:01:46
栏目: 编程语言

使用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!"

0
看了该问题的人还看了