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)