For details about how to delete files or folder instances in python,

Source: Internet
Author: User

For details about how to delete files or folder instances in python,

Delete files or folders using python

I recently learned about Python and the content of web pages. When I learned how to delete folders or files in Python, I think this article is good and I recommend it to you.

Instance code, carefully read the annotation details:

#-*-Coding: UTF-8-*-# Sometimes you want to delete files or folders in a folder # This small class is useful to delete files or folders # Sometimes you want to see the deletion results when deleting files, so now we have this Deleter # Some of the following file annotations use the Doxygen style # in English. Do not change it back to Chinese. 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 try: OS. rmdir (FolderName) self. log ("Folder deleted: % s" % (FolderName) failed T: 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) failed T: 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 = folder 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

Thank you for reading this article. I hope it will help you. Thank you for your support for 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.