Python Delete file or folder implementation code

Source: Internet
Author: User

python implementation deletes a file or folder

Recently learn Python's knowledge of their own learning to crawl the content of the Web page knowledge, etc., when the study of Python Delete folder or file knowledge, I think this article is good, recommended to everyone.

Instance code, look closely at the annotation details:

#-*-Coding:utf-8-*-# sometimes to delete files or folders in a folder # This small class used to delete files or folders is quite useful # when you delete a file, you sometimes want to see the result of the deletion, so you have this deleter. # Some of the following file comments are in the Doxy 
Gen's style # in English, the first will 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 # # del a folder # @param FolderName folder to delete #<br/> #Ex 
  Ample: #<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.DELETEF Ile (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 Folderlis T a list of folder to delete #<br/> #Example: #<pre> # t = Common () # foldernamelist = [R ' Tem  P1 ', R ' Temp2 '] # t.deletefolders (foldernamelist) #</pre> def deletefolders (self, foldernamelist): for FolderName in Foldernamelist:self. DeletefOlder (FolderName) return # del a list of files # @param folderlist A list of files to Delete #<br/&gt 
  ; #Example: #<pre> # T = Common () # foldernamelist = [R ' Temp1.txt ', R ' Temp2.txt '] # t.deletefiles (filen amelist) #</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

Above is the Python delete file or folder implementation of the entire code, I hope to help.

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.