圆月山庄资源网 Design By www.vgjia.com
python 实现删除文件或文件夹
最近自己学习Python 的知识,自己学习抓取网页的内容知识等,在学习的时候Python 删除文件夹或者文件知识的时候,觉得本篇内容不错,推荐给大家。
实例代码,仔细看注释明细:
#-*- coding: UTF-8 -*- # 有时候要删除文件夹里文件或文件夹 # 这个小类用来删除文件或文件夹还是相当有用的 # 删除文件时有时希看到删除的结果,于是就有了这个Deleter了 # 下面的一些文件注释用的是Doxygen的样式 # 用了英文,先就不改回中文吧。 class Deleter(): def __init__(self, ToPrint=True, Logger=None): self.Logger = Logger self.ToPrint = ToPrint return def Log(self, Str): if self.Logger: self.Logger.Log(Str) if self.ToPrint: print Str return ## Delete a folder # @param FolderName folder to delete #<br/> #Example: #<pre> # T = Common() # FolderName = r'Temp' # T.DeleteFolder(FolderName) #</pre> def DeleteFolder(self, FolderName): '''''delete files and folders''' for Item in os.listdir(FolderName): TempPath = os.path.join(FolderName, Item) if os.path.isfile(TempPath): self.DeleteFile(TempPath) pass elif os.path.isdir(TempPath): self.DeleteFolder(TempPath) pass else: self.Log("Not a file or folder: %s" % (FolderName)) pass pass try: os.rmdir(FolderName) self.Log("Folder deleted: %s" % (FolderName)) except: self.Log("Failed to delete folder: %s" % (FolderName)) pass return ## Delete a file # @param FileName file to delete #<br/> #Example: #<pre> # T = Common() # FolderName = r'TempFile.txt' # T.DeleteFile(FileName) #</pre> def DeleteFile(self, FileName): try: os.remove(FileName) self.Log("File deleted: %s" % (FileName)) except: self.Log("Failed to delete file: %s" % (FileName)) pass return ## Delete a list of folders # @param FolderList a list of folder to delete #<br/> #Example: #<pre> # T = Common() # FolderNameList = [r'Temp1', r'Temp2'] # T.DeleteFolders(FolderNameList) #</pre> def DeleteFolders(self, FolderNameList): for FolderName in FolderNameList: self.DeleteFolder(FolderName) return ## Delete a list of files # @param FolderList a list of files to delete #<br/> #Example: #<pre> # T = Common() # FolderNameList = [r'Temp1.txt', r'Temp2.txt'] # T.DeleteFiles(FileNameList) #</pre> def DeleteFiles(self, FileNameList): for FileName in FileNameList: self.DeleteFile(FileName) return Op = Deleter() Op.DeleteFile('a.txt') # Failed to delete file: a.txt
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
圆月山庄资源网 Design By www.vgjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
圆月山庄资源网 Design By www.vgjia.com
暂无评论...
更新日志
2024年11月08日
2024年11月08日
- 雨林唱片《赏》新曲+精选集SACD版[ISO][2.3G]
- 罗大佑与OK男女合唱团.1995-再会吧!素兰【音乐工厂】【WAV+CUE】
- 草蜢.1993-宝贝对不起(国)【宝丽金】【WAV+CUE】
- 杨培安.2009-抒·情(EP)【擎天娱乐】【WAV+CUE】
- 周慧敏《EndlessDream》[WAV+CUE]
- 彭芳《纯色角3》2007[WAV+CUE]
- 江志丰2008-今生为你[豪记][WAV+CUE]
- 罗大佑1994《恋曲2000》音乐工厂[WAV+CUE][1G]
- 群星《一首歌一个故事》赵英俊某些作品重唱企划[FLAC分轨][1G]
- 群星《网易云英文歌曲播放量TOP100》[MP3][1G]
- 方大同.2024-梦想家TheDreamer【赋音乐】【FLAC分轨】
- 李慧珍.2007-爱死了【华谊兄弟】【WAV+CUE】
- 王大文.2019-国际太空站【环球】【FLAC分轨】
- 群星《2022超好听的十倍音质网络歌曲(163)》U盘音乐[WAV分轨][1.1G]
- 童丽《啼笑姻缘》头版限量编号24K金碟[低速原抓WAV+CUE][1.1G]