Linux 拨号vps windows公众号手机端

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

lewis 6年前 (2019-03-27) 阅读数 12 #程序编程
文章标签 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!"

版权声明

本文仅代表作者观点,不代表米安网络立场。

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

热门