python 实现删除文件或文件夹实例详解
作者:lqh 时间:2021-03-23 03:16:37
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
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
标签:python,删除文件,文件夹


猜你喜欢
MySQL 通过索引优化含ORDER BY的语句
2010-03-25 10:28:00
Vuex实现购物车小功能
2024-05-21 10:29:07

python实现简单学生信息管理系统
2022-01-28 12:40:00

python 类的基础详解与应用
2021-01-04 08:57:37
asp会员系统如何实现“忘记密码”
2007-09-19 12:17:00
Vue.js实现文章评论和回复评论功能
2024-05-29 22:20:31

ant-design-vue中的select选择器,对输入值的进行筛选操作
2024-04-26 17:41:26

基于Python实现自动扫雷详解
2023-09-07 23:31:20

利用python实现查看溧阳的摄影圈
2021-09-05 21:33:16

Python的Bottle框架中返回静态文件和JSON对象的方法
2023-11-07 20:14:41

python中对数据进行各种排序的方法
2022-05-31 15:26:55
python将unicode和str互相转化的实现
2023-07-17 21:36:34
如何在Django项目中引入静态文件
2021-10-09 11:24:52

python3.7中安装paddleocr及paddlepaddle包的多种方法
2022-08-11 00:00:57

在Windows系统上安装Cygwin搭建Swoole测试环境的图文教程
2022-11-13 21:19:34

不能安装SQL Server 2005的问题及解决方法
2008-12-26 17:41:00
django中的数据库迁移的实现
2024-01-18 07:35:00

Mysql InnoDB多版本并发控制MVCC详解
2024-01-23 16:46:25

laravel 实现阿里云oss文件上传功能的示例
2023-06-13 20:39:26

python使用sqlite3时游标使用方法
2022-10-10 09:29:16