
python支持对象和函数
1. python是解释型语言,逐行运行
2. 对象调用
例子:删除文件的一个例子
2.1 先定义一个类 class MyApp:
2.2 import 引用要用到的模块
__author__ = 'Bright' import shutil
import os """
* 对象操作实战
* 上演出多个文件
""" class MyApp:
def __init__(self, root):
self.root = root
print('根目录%s' % self.root) def DeleteFile(self, path): # 删除单个文件
if path != '':
path = path.strip() # 去空
path = str.format("{0}\\{1}", self.root, path)
if os.path.isfile(path):
os.remove(path)
elif os.path.isdir(path):
shutil.rmtree(path, True)
print('删除成功') def DeleteFiles(self, path): # 删除多个文件
path = str.format("{0}\\{1}", self.root, path)
if ',' in path:
paths = path.split(',')
for item in paths:
self.DeleteFile(item)
else:
self.DeleteFile(path)
print('删除成功') if __name__ == '__main__':
root = 'J:\\Download'
app = MyApp(root)
gameName = input('请输入游戏名:')
app.DeleteFile(gameName)
3.python文件的__name__是缺省值,默认是"__main__",如果import 调用模块的话,__name__一般是模块文件名,不带路径或扩展名,比如 。