脚本专栏 
首页 > 脚本专栏 > 浏览文章

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

(编辑:jimmy 日期: 2024/11/19 浏览:3 次 )

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 正则表达式入门(中级篇)
下一篇:Python 正则表达式入门(初级篇)
一句话新闻
一文看懂荣耀MagicBook Pro 16
荣耀猎人回归!七大亮点看懂不只是轻薄本,更是游戏本的MagicBook Pro 16.
人们对于笔记本电脑有一个固有印象:要么轻薄但性能一般,要么性能强劲但笨重臃肿。然而,今年荣耀新推出的MagicBook Pro 16刷新了人们的认知——发布会上,荣耀宣布猎人游戏本正式回归,称其继承了荣耀 HUNTER 基因,并自信地为其打出“轻薄本,更是游戏本”的口号。
众所周知,寻求轻薄本的用户普遍更看重便携性、外观造型、静谧性和打字办公等用机体验,而寻求游戏本的用户则普遍更看重硬件配置、性能释放等硬核指标。把两个看似难以相干的产品融合到一起,我们不禁对它产生了强烈的好奇:作为代表荣耀猎人游戏本的跨界新物种,它究竟做了哪些平衡以兼顾不同人群的各类需求呢?