摘要:python中对文件、文件夹(文件操作函数)的操作需要涉及到os模块和shutil模块。得到当前工作目录,即当前Python脚本工作的目录路径: os.getcwd()返回指定目录下的所有文件和目录名:os.listdir()函数用来删除一个文件:os.remove()删除多个目录:os.removedirs(r“c:\python”)检验给出的路径是否是一个文件:os.path.isfile()检验给出的路径是否是一个目录:os.path.isd
2017年02月11日
文件创建,追加内容,关闭
摘要:创建文件:>>> file('test.txt','w') #如果文件不存在,以w形式打开会创建文件<open file 'test.txt', mode 'w' at 0x7f1111597660> #向文件中写入内容>>> f=file('test.txt','w')>>> f.write('Today is a good day') &nb
分类:Python
2017年02月11日
装饰器-3
摘要:[root@node110 decorator]# cat temp_decorator.py #!/usr/bin/env pythonimport timedef time_counter(func): def wrapper(): start =time.time() func() end =time.time() print 'Thisfunction costs:',end-start return wrapper #sayhi=time_counter(sayhi)#sayhi()@time_
分类:Python
2017年02月11日
装饰器-2
摘要:[root@node110 decorator]# cat temp.py #!/usr/bin/env pythonimport timedef sayhi(): print 'hi yoursister...' time.sleep(0.1) def time_counter(func): def wrapper(): start =time.time() func() end =time.time() print 'Thisfunction costs:',end-start retur
分类:Python
2017年02月11日
装饰器-1
摘要:查看函数的执行时间:[root@node110 decorator]# cat exec_time.py #!/usr/bin/env pythonimport timedef sayhi(): start=time.time() print 'hi yoursister...' time.sleep(0.1) end= time.time() print 'Thisfunction costs:',end-start sayhi()
分类:Python
2017年02月11日
Python 与 Mysql交互
摘要:Python 与Mysql交互#!/usr/bin/envpythonimport MySQLdbtry: conn = MySQLdb.connect(host='192.168.198.110',user='root',passwd='123456',port=3306,db='mysql') cur=conn.cursor() cur.execute('select user,host,password frommysql.user') qur_result=cur.fetchall() for record in qur_result: print record cur.
分类:Python
2017年02月07日
Python-集合类型
摘要: 集合类型 把 set 称做由不同的元素组成的集合,集合(set)的成员通常被称做集合元素(set elements)。Python 把这个概念引入到它的集合类型对象里。集合对象是一组无序排列的可哈希的值 但是因为集合本身是无序的,你不可以为集合创建索引或执行切片(slice)操作,也没有键(keys)可用来获取集合中元素的值。 集合(sets)有两种不同的类型,可变集合(set
分类:Python
2017年02月07日
Python 元组
摘要:Python元组Python的元组与列表类似,不同之处在于元组的元素不能修改。元组使用小括号,列表使用方括号。元组创建很简单,只需要在括号中添加元素,并使用逗号隔开即可。如下实例:tup1 = ('physics', 'chemistry', 1997, 2000);tup2 = (1, 2, 3, 4, 5 );tup3 = "a", "b", "c", "d";创建空元组tup1 = ();元组中只包含一个元素时,需要
分类:Python
2017年02月07日
list-模糊查询姓名并高亮程序
摘要:模糊查询姓名并高亮#!/usr/bin/env pythonstaff_dic={}f=file('stu_info.txt')for line in f.xreadlines(): stu_id,stu_name,mail,company,title,phone=line.split() staff_dic[stu_id]=[stu_name,mail,company,title,phone] while True: query=raw_input('\033[32;1mPlease input the querystring\033[0m').strip() if len(query)< 3: &nb
分类:Python
2017年02月07日
list-购物程序
摘要:#!/usr/bin/env pythonimport syssalary=int(raw_input('Please input your salary:'))products=[['Iphone',5800],['MacPro',12000],['NB Shoes',680],['Cigrate',48],['MX4',2500]]#create a shopping listshopping_list=[]while True: for p in products: print products.index(p), p[0],p[1] choice = raw_input("\033[32;1mPlease choo