##重要说明###copy过来的代码格式有问题,在使用过程中注意调整编码格式 !
日志方法:
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
#文件根据时间倒序排列
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 结束执行\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结束,按任意键退出....')#检测到Ctrl+c组合键结束运行
Fromdate转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("最终转换的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)
#转换为字符串
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("时间戳转日期:"+"%Y:%m:%d %H:%M:%S", localtime))