python 实现删除文件或文件夹实例详解

yipeiwu_com5年前Python基础

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 

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

相关文章

利用nohup来开启python文件的方法

python文件可以直接使用命令python xxx.py来启动文件,但是这样会有一个弊端,就是关闭ssh连接,python文件就会自动的进行停止。 所以需要使用利用nohup来开启py...

Python读取xlsx文件的实现方法

脚本如下: from openpyxl import load_workbook workbook = load_workbook(u'/tmp/test.xlsx') #找到需...

详解Python中最难理解的点-装饰器

详解Python中最难理解的点-装饰器

本文将带领大家由浅入深的去窥探一下,这个装饰器到底是何方神圣,看完本篇,装饰器就再也不是难点了. 一、什么是装饰器 网上有人是这么评价装饰器的,我觉得写的很有趣,比喻的很形象 每个...

Python模拟用户登录验证

Python模拟用户登录验证

本文实例为大家分享了Python模拟用户登录验证的具体代码,供大家参考,具体内容如下 1.功能简介 此程序模拟用户登录验证的过程,实现用户名输入、黑名单检测、用户有效性判别、密码输入及验...

浅述python中深浅拷贝原理

前言 在c++中参数传递有两种形式:值传递和引用传递。这两种方式的区别我不在此说,自行补上,如果你不知道的话。我先上python代码,看完我们总结一下,代码如下: # copy m...