Python implementation delete a file or folder instance in detail

Source: Internet
Author: User
Python implementation deletes files or folders

Recently learn Python's knowledge, and learn how to crawl the content of the Web page knowledge, in the study when Python delete folder or file knowledge, I think this article content is good, recommend to everyone.

Instance code, look closely at the comment details:

#-*-Coding:utf-8-*-# sometimes to delete files or folders in a folder # This small class is used to delete a file or folder or it is quite useful to delete the file when you sometimes hope to see the result of the deletion, so there is this deleter # Some of the following file comments are used Doxygen The style # used in English, first do not change back to Chinese bar. 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 DELETEFO Lder (self, FolderName): "' Delete files and folders ' for Item in Os.listdir (FolderName): TempPath = OS.PA Th.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)) passPass 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 the FileName file to Delete #<br/> #Example: #<pre> # T = Common () # FolderName = R ' TempFile.txt ' # t.deletefile (Fi Lename) #</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 ' Te MP2 '] # t.deletefolders (foldernamelist) #</pre> def deletefolders (self, foldernamelist): for FolderName I N 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 ' Temp 1.txt ', R ' Temp2.txt '] # t.deletefiles (filenamelist) #</pre> def deletefiles (self, filenamelist): for File Name in Filenamelist:self. DeleteFile (FileName) return Op = Deleter () op.deletefile (' A.txt ') # Failed to delete file:a.txt

Thank you for reading, hope to help everyone, thank you for the support of this site!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.