扫描二维码下载沐宇APP

沐宇

微信扫码使用沐宇小程序

沐宇

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

扬州沐宇科技
2021-03-08 14:01:46
python

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

扫码添加客服微信