Object manipulation of Python (i)

Source: Internet
Author: User

Python supports objects and functions
1. Python is an interpreted language that runs on a line-by-row basis

2. Object invocation

Example: an example of deleting a file

2.1 First define a class: MyApp.

2.2 Import references the module to be used

__author__='Bright'ImportShutilImportOS"""* Object Operation Combat * Multiple files on the show"""classMyApp:def __init__(self, root): Self.root=RootPrint('root directory%s'%self.root)defDeleteFile (self, Path):#Delete a single file        ifPath! ="': Path= Path.strip ()#Go EmptyPath = Str.format ("{0}\\{1}", Self.root, Path)ifos.path.isfile (path): Os.remove (path)elifOs.path.isdir (path): Shutil.rmtree (Path, True)Print('Delete succeeded')    defDeletefiles (self, Path):#Delete multiple FilesPath = Str.format ("{0}\\{1}", Self.root, Path)if ',' inchpath:paths= Path.split (',')             forIteminchpaths:self. DeleteFile (item)Else: Self. DeleteFile (PATH)Print('Delete succeeded')if __name__=='__main__': Root='J:\\download'app=MyApp (Root) gamename= Input ('Please enter the game name:') app. DeleteFile (gamename)

The __name__ of the 3.python file is the default value, the default is "__main__", if the import call module, __NAME__ is generally the module file name, without the path or extension, for example.

Object manipulation of Python (i)

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.