##重要說明###copy過來的代碼格式有問題赤兴,在使用過程中注意調(diào)整編碼格式 !
日志方法:
import sys
class Logger():
#fileN為日志路徑
? ? def __init__(self, fileN="python_sendreport.log"):
self.terminal = sys.stdout
self.log =open(fileN, "a")
#寫日志方法
? ? def write(self, message):
self.terminal.write(message)
self.log.write(message)
def flush(self):
pass
查看指定路徑下所有類型文件
import os
class read_execlfile(object):
# 查詢當前路徑下所有xlsx
? ? def search(self, path, filename):
result = []
for itemin os.listdir(path):
item_path = os.path.join(path, item)
if os.path.isdir(item_path):
read_execlfile().search(item_path, filename)
elif os.path.isfile(item_path):
if filenamein item:
result.append(item_path)
return result
if __name__ =='__main__':
#獲取當前路徑
? ? #path = os.path.split(os.path.realpath(__file__))[0]
? ? # 報告文件地址
? ? EX_PATH = read_execlfile().search(r'C:\Users\Lenovo\Desktop\個人', ".xlsx")
print(EX_PATH)
當前路徑下所有文件倒序排列
import os,time,glob
#文件根據(jù)時間倒序排列
????def search_all_files_return_by_time_reversed(path, reverse=True):
????return sorted(glob.glob(os.path.join(path, '*')), key=lambda x: time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(os.path.getctime(x))), reverse=reverse)
if __name__ =='__main__':
????res=search_all_files_return_by_time_reversed(r"C:\Users\Lenovo\Desktop\個人")
????print(res)
獲取當前分辨率下坐標:
首先安裝:pip install?pyautoguias?
cmd?運行
import os
import? time
import? pyautoguias pg
try:
while True:
print("按下組合鍵 Ctrl+C 結(jié)束執(zhí)行\(zhòng)n")
sW, sH = pg.size()#獲取屏幕的尺寸(像素)screenWidth,screenHeight
? ? ? ? print("當前屏幕分辨率:\n"+str(sW)+','+str(sH)+'\n')#打印屏幕分辨率
? ? ? ? x,y = pg.position()#獲取當前鼠標的坐標(像素)
? ? ? ? print("當前鼠標坐標(5秒刷新):\n" +str(x).rjust(4)+','+str(y).rjust(4))#打印鼠標坐標值
? ? ? ? time.sleep(5)#等待5秒
? ? ? ? os.system('cls')#清屏
except KeyboardInterrupt:
print('\n結(jié)束,按任意鍵退出....')#檢測到Ctrl+c組合鍵結(jié)束運行
Fromdate轉(zhuǎn)json
import re
def fromdate_json(text):
#替換字符串
? ? s1=re.sub(r'=', ":", text)
print(s1)
#以對應字符串完成分割
? ? s2=s1.split('&',-1)
print(s2)
#定義字典
? ? dic={}
for i in s2:
#以對應字符串切割
? ? ? ? keys=i.split(':')
#將值更新至字典中
? ? ? ? dic.update({keys[0]: keys[1]})
print("最終轉(zhuǎn)換的JSON格式:\n{json}".format(json=dic))
if __name__ =='__main__':
text ='isNeiCai=0&imageSize=s&key=proafe0e15e66094f0f95a1c1f97cceb164-375993-login×tamp=1584926701449&callback=jsonp_031527008695759373'
? ? fromdate_json(text)
正則表達式:
import re
text="isNeiCai=0&imageSize=s&key=proafe0e15e66094f0f95a1c1f97cceb164-375993-login&"
#正則表達式提取值部分(.*?)
httpHost = re.findall(r'isNeiCai=(.*?)&', text, re.S)
#輸出為例表格式
print(httpHost)
#轉(zhuǎn)換為字符串
httpHost =''.join(httpHost)
#輸出為字符串格式
print(httpHost)
time模塊常用日期時間處理
import time
print("時間戳:{time}".format(time=time.time()))
print("10位時間戳:{time}".format(time=int(time.time())))
print("13位時間戳:{time}".format(time=int(round(time.time()*1000))))
print("年月日時:"+time.strftime("%Y%m%d%H%M%S", time.localtime(time.time())))
print("標準年月日時格式化:"+time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
timetamp=1591689695
localtime=time.localtime(timetamp)
print(time.strftime("時間戳轉(zhuǎn)日期:"+"%Y:%m:%d %H:%M:%S", localtime))