時間模塊
#user:Administrator
#date:2019/11/13 0013
import time
# print(help(time))
# print(time.time()) #結果:1573655126.5573525 從1970年到現在的時間
# time.sleep(3)
# print(time.clock()) #計算CPU執行時間
# print(time.gmtime()) #世界標準時間time.struct_time(tm_year=2019, tm_mon=11, tm_mday=13, tm_hour=14, tm_min=39, tm_sec=38, tm_wday=2, tm_yday=317, tm_isdst=0)
# print(time.localtime()) #本地時間
# ss = time.localtime()
# print(time.strftime('%Y-%m-%d %H:%M:%S',ss)) #結果:2019-11-13 22:54:00
# a = (time.strptime('2019-11-13 22:54:00','%Y-%m-%d %H:%M:%S')) #時間轉換
# #結果:time.struct_time(tm_year=2019, tm_mon=11, tm_mday=13, tm_hour=22, tm_min=54, tm_sec=0, tm_wday=2, tm_yday=317, tm_isdst=-1)
# print(a.tm_yday) #一年過去了多少天
# print(a.tm_mon) #幾月份
# print(a.tm_hour) #幾點
# print(a.tm_year) #年份
# print(time.ctime()) #Wed Nov 13 23:09:46 2019
# import datetime
# print(datetime.datetime.now())
#結果:2019-11-13 23:15:13.101352
隨機數模塊
import random
# print(random.random()) #0.7389143790816882
#
# print(random.randint(1,10)) #1-10隨機打印
# print(random.choice('hello world'))
# print(random.choice(['helld',3,[1,2]]))
# print(random.sample([[1,2,3],'12',4],2)) #隨機打印2個元素
# print(random.randrange(1,2)) #不包括後面元素
#隨機5位驗證碼
def v_code():
code=''
for i in range(5):
add = random.choice([random.randrange(10), chr(random.randrange(65, 91))])
code +=str(add)
print(code)
v_code()
os模塊
import os
# print(os.getcwd()) #獲取當前Python腳本路徑
#
# os.chdir('F:\網絡工程')
# print(os.getcwd()) #更改腳本路徑
# os.makedirs('abc\\hao\\xue') #遞歸創建目錄
# os.mkdir('aa') #只能創建一個
# os.removedirs('abc\\hao\\xue') #遞歸刪除空文件
# os.rmdir('abc\\hao\\xue') #只能刪除一個
# os.listdir() #顯示目錄下的文件
# os.remove() #刪除文件,不能刪除文件夾
# os.rename('aa','www') #重命名文件、目錄
# info = os.stat('.\\www')
# print(info.st_size) #文件大小
# os.sep #輸出操作系統特定的路徑分隔符,win'\' ,linux'/'
# os.linesep #換行分隔符
# os.pathsep #變量分隔符
# print(os.system("dir")) #執行shell命令
# os.environ #獲取環境變量
# os.path.split() #路徑和文件分割,組成一個元組
# print(os.path.dirname('F:\網絡工程\python\PyCharm 2017.3.3\全棧學習\venv\Scripts')) #路徑的上一層
# os.path.join() #拼接
sys模塊
import sys
# print(sys.argv) #命令行list,第一個元素是程序本身的路徑
# print(sys.path)
# print(sys.platform) #顯示當前系統win32爲windows系統,
#
# import os
# if sys.platform=='win32':
# os.system('dir') #win列出當前目錄文件
# else:
# os.system('ls -lh') #linux列出當前目錄文件
hashlib模塊
import hashlib
#md5算法
# m = hashlib.md5()
# print(m) #輸出結果:<md5 HASH object @ 0x0031D488>
# m.update('hello world'.encode('utf8'))
# print(m.hexdigest()) #輸出結果:5eb63bbbe01eeed093cb22bb8f5acdc3
#sha算法
# s=hashlib.sha256()
# s.update('hello world'.encode('utf8'))
# print(s.hexdigest())
#輸出結果:b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9
logging模塊
logging.basicConfig參數
import logging
# LOG_FORMAT = "%(asctime)s %(name)s %(levelname)s %(pathname)s %(message)s "#配置輸出日誌格式
# DATE_FORMAT = '%Y-%m-%d %H:%M:%S %a ' #配置輸出時間的格式,注意月份和天數不要搞亂了
# logging.basicConfig(level=logging.DEBUG,
# format=LOG_FORMAT,
# datefmt = DATE_FORMAT ,
# filename=r"test.log" #有了filename參數就不會直接輸出顯示到控制檯,而是直接寫入文件
# )
# logging.debug("msg1")
# logging.info("msg2")
# logging.warning("msg3")
# logging.error("msg4")
# logging.critical("msg5")
#輸出結果:
2019-11-24 16:05:59 Sun root DEBUG E:/python/fullstack_s2/weeks2/module/logging_module.py msg1
2019-11-24 16:05:59 Sun root INFO E:/python/fullstack_s2/weeks2/module/logging_module.py msg2
2019-11-24 16:05:59 Sun root WARNING E:/python/fullstack_s2/weeks2/module/logging_module.py msg3
2019-11-24 16:05:59 Sun root ERROR E:/python/fullstack_s2/weeks2/module/logging_module.py msg4
2019-11-24 16:05:59 Sun root CRITICAL E:/python/fullstack_s2/weeks2/module/logging_module.py msg5
#logger
# import logging
# #創建logger
# logger = logging.getLogger()
# #創建文件對象
# fh = logging.FileHandler('test.log')
# #創建屏幕標準對象
# ch = logging.StreamHandler()
# #設置日誌格式
# formatter = logging.Formatter('%(asctime)s %(name)s %(levelname)s %(message)s')
# #文件調用formatter日誌格式
# fh.setFormatter(formatter)
# #屏幕輸出調用formatter日誌格式
# ch.setFormatter(formatter)
# #logger添加文件、屏幕輸出
# logger.addHandler(fh)
# logger.addHandler(ch)
# logger.setLevel(logging.DEBUG) #顯示所有等級的日誌
#
# logger.debug('debug massage')
# logger.info('info massage')
# logger.warning('warning massage')
# logger.error('error massage')
# logger.critical('critical massage')
#輸出結果:
配置文件模塊
import configparser
config = configparser.ConfigParser()
# config['DEFAULT'] = {'compression':'yes',
# 'serveraliveinterval':'45',
# 'CompressionLevel':'9'
# }
#
# config['bitbucket.org'] = {
# 'user':'hao'
#
# }
#
# config['www'] = {
# 'Port':'22'
#
# }
#
# with open('test.ini','w',encoding='utf8') as configfile:
# config.write(configfile)
#讀取配置文件(default默認不顯示)
config.read('test.ini')
print(config.sections())
#輸出結果:['bitbucket.org', 'www']
#test.ini輸出結果:
#讀取配置文件(default默認不顯示)
config.read('test.ini')
# print(config.sections())
# #輸出結果:['bitbucket.org', 'www']
# print(config.defaults()) #default模塊下的值
# for key in config['www']:
# print(key)
#輸出結果: 默認dedault模塊的key也跟着打印出來
'''
port
serveraliveinterval
compression
compressionlevel
'''
#刪除www模塊,覆蓋原文件
config.remove_section('www')
config.set('bitbucket.org','user','xiaoxue') #修改user
config.remove_option('bitbucket.org','user') #刪除bitbucket.org模塊下的user
config.write(open('test.ini','w'))