编写一个进度条
1 #!/usr/bin/env python3 2 3 import sys, time 4 for i in range(31): 5 sys.stdout.write(' ')#每次固定在开头,清空原行 6 sys.stdout.write("%s\%% | %s" % (int(i / 30 * 100), int(i / 30 * 100) * "*"))#int(i / 30 * 100) * "*"代表打印多少个* 7 sys.stdout.flush()#强制刷新屏幕 8 time.sleep(0.3)#等待多少秒
1 import sys,time 2 for i in range(1,100): 3 if i < 9:#判断是否是占两位数 4 sys.stdout.write('#'+'%'+str(i+1)+"")#进度条退两格,为连接下一个#腾地方 5 sys.stdout.flush()#刷新显示 6 time.sleep(0.5) 7 else: 8 sys.stdout.write('#' + '%' + str(i + 1) + "") 9 sys.stdout.flush() 10 time.sleep(0.5)
1 time.sleep(5) 2 print("wake up ....") 3 print(time.time())#时间戳1970 1 1到现在的过去多少秒 4 print(time.ctime())#返回当前时间 5 print(time.ctime(time.time()-86400))#将时间转换成字符串昨天的这个时间 6 time_obj=time.gmtime(time.time()-86400) 7 print(time_obj) 8 print("%d-%f-%s %s:%s" % (time_obj.tm_year,time_obj.tm_mon,time_obj.tm_mday,time_obj.tm_hour,time_obj.tm_min)) #格式化 9 # import sys
"""
wake up ....
1507429553.1381178
Sun Oct 8 10:25:53 2017
Sat Oct 7 10:25:53 2017
time.struct_time(tm_year=2017, tm_mon=10, tm_mday=7, tm_hour=2, tm_min=25, tm_sec=53, tm_wday=5, tm_yday=280, tm_isdst=0)
2017-10.000000-7 2:25
"""
1 import datetime 2 print(datetime.datetime.now())#返回当前系统时间 3 print(datetime.date.fromtimestamp(time.time()))#时间戳直接转成日期格式 4 print(datetime.datetime.now() + datetime.timedelta(3))#当前时间加三天 5 print(datetime.datetime.now() + datetime.timedelta(-3))#当前时间减三天 6 print(datetime.datetime.now() + datetime.timedelta(hours=3))#当前时间加3小时 7 print(datetime.datetime.now() + datetime.timedelta(minutes=30))#当前时间加30分钟
"""
运行结果
2017-10-08 11:11:59.362852
2017-10-08
2017-10-11 11:11:59.362852
2017-10-05 11:11:59.362852
2017-10-08 14:11:59.362852
2017-10-08 11:41:59.362852
"""
sys模块
sys.path()其中有一个目录Python35\lib\site-packages将写好的py放入其中,import 就可以导入,一般import导入则在当前目录,如果想在任何地方都可以导入就放入site-packages中
sys.argv运行后面带的参数
1 import sys 2 print(sys.path) 3 print(sys.argv) 4 if sys.argv[1] == 'sleep': 5 print("hello") 6 else: 7 print("wakeup")
"""
H:python17w4>python timemodle.py sleep
['timemodle.py', 'sleep']
hello
H:python17w4>python timemodle.py sleep
['H:\python17\w4', 'C:\Python27\lib\site-packa
ges\paramiko-1.13.0-py2.7.egg', 'C:\Windows\syst
em32\python27.zip', 'C:\Python27\DLLs', 'C:\Pyt
hon27\lib', 'C:\Python27\lib\plat-win', 'C:\Py
thon27\lib\lib-tk', 'C:\Python27', 'C:\Python27
\lib\site-packages']
['timemodle.py', 'sleep']
如果想安装django
在Scripts目录中找到pip.exe
在命令行中运行pip.ext install django
卸载pip.exe uninstall django
pickle模块
dumps
loads
初始化,写一个字典存起来
1 #!/usr/bin/env python3 2 import pickle 3 account_dic={ 4 1000:{ 5 'name':"aric", 6 'passwod':'123', 7 'bank_acc':1234556, 8 'balance':15000 9 }, 10 11 1001: { 12 'name': "robert", 13 'passwod': '456', 14 'bank_acc': 334589, 15 'balance': 15000 16 }, 17 18 } 19 f=open('accout.db','wb') 20 f.write(pickle.dumps(account_dic))#与pickle.dumps(account_dic,f)相同
#f.write(bytes(json.dumps(account_dic),encoding='utf-8'))#如果用json
21 f.close()
修改balance程序
1 #!/usr/bin/env python3 2 import pickle 3 file_name='accout.db' 4 f=open(file_name,'rb') 5 account_dic=pickle.loads(f.read()) 6 f.close() 7 account_dic[1000]['balance'] -=500 8 print(account_dic) 9 f1=open(file_name,'wb') 10 f1.write(pickle.dumps(account_dic)) 11 f1.close()
查看之后数据修改的情况
1 #!/usr/bin/env python3 2 import pickle 3 f=open('accout.db','rb') 4 account_db = pickle.loads(f.read())#可用account_db=picle.load(f)
#account_dic=json.loads(f.read().decode("utf-8"))如果用json
5 print(account_db)
json将字典写入文件中
1 import json 2 data={101:101,102:"cat"} 3 f=open('db','w',encoding='utf-8') 4 json.dump(data,f) 5 f.close()
读文件
1 #!/usr/bin/env python3 2 import json 3 # data={101:101,102:"cat"} 4 # f=open('db','w',encoding='utf-8') 5 # json.dump(data,f) 6 # f.close() 7 f=open('db','r',encoding='utf-8') 8 #n=json.loads(f.read()) 9 n=json.load(f) 10 print(n) 11 f.close()