脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - python 实现删除文件或文件夹实例详解

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

2020-09-13 11:46脚本之家 Python

这篇文章主要介绍了python 实现删除文件或文件夹实例详解的相关资料,这里附有实例代码,需要的朋友可以参考下

python 实现删除文件文件夹

          最近自己学习Python 的知识,自己学习抓取网页的内容知识等,在学习的时候Python 删除文件夹或者文件知识的时候,觉得本篇内容不错,推荐给大家。

实例代码,仔细看注释明细:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#-*- 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

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

延伸 · 阅读

精彩推荐