day6作业

1 logs目录下,有一部分文件是空的
#删除logs目录下,所有空文件 先从文件名里获取到文件的日期,然后吧日期转成时间戳 ,再获取到当前的时间戳,进行比较
#删除5天前的文件
需求分析
1 os.walk()获取到所有以.log结尾的文件
2 判断文件的大小 os.getsize()
3 先从文件名里面获取到文件的日期,然后把日期转成时间戳
4 获取到5天前的时间戳,进行比较

import os,time,datetime
import time
def strToTimestamp(str=None,format='%Y%m%d%H%M%S'):
    #20180421165643
    if str:    #如果传了格式化好的时间
        tp = time.strptime(str,format)   #转成时间元祖
        res = time.mktime(tp)   #转成时间戳
    else:
        res = time.time()   #默认取当前的时间戳
    return int(res)

def clean_log(path):
    for abs_path,dir,file in os.walk(path):
        #print(file)
        for f in file:
            #print(f)
            if f.endswith('.log'):   #判断是不是文件
                full_path = os.path.join(abs_path,f)  #拼接文件绝对路径
                f_date = f.split('_')[1].split('.')[0]  #分割文件名取时间
                f_date_time_stamp = strToTimestamp(f_date,'%Y-%m-%d')#文件时间转时间戳
                five_day = str(datetime.date.today()+datetime.timedelta(days=-5))#获取5天前时间
                five_day_stamp = strToTimestamp(five_day, '%Y-%m-%d')  #5天前时间转时间戳
                if os.path.getsize(full_path) ==0 or f_date_time_stamp<five_day_stamp:
                    os.remove(full_path)
clean_log('./logs')

 

2写代码实现,把数据库里面的stu表中的数据,导出到excel中   表结构 id  name  sex
需求分析
1 连接数据库,写好sql 查数据 pymysql select * from stu;
2 写excel xlwt
import pymysql ,xlwt
def my_db(sql):
    import pymysql
    coon=pymysql.connect(host='118.24.3.40',user='jxz',passwd='123456',port=3306,db='jxz',charset='utf8')
    cur = coon.cursor()#建立游标
    cur.execute(sql)#执行sql
    if sql.strip()[:6].upper()=='SELECT':  #判断select是否有空和大写,转成大写
        res = cur.fetchall()
    else:
        coon.commit()
        res = 'ok'
    cur.close()
    coon.close()
    return res
def export(excel_name):
    sql = 'select * from stu;'
    data = my_db(sql)
    book = xlwt.Workbook()  #新建一个excel
    sheet = book.add_sheet('sheet1')   #加sheet页
    title= ['id','name','sex']
    col = 0   #
    for t in title:
        sheet.write(0,col,t)  #写表头
        col+=1
    row = 1
    for d in data:
#print(d) #(8, 'LYL', '女') col
= 0 for line in d: sheet.write(row,col,line) col+=1 row+=1 book.save('stu.xls') export('stu.xls')

 3 

import hashlib,pymysql,datetime
def my_db(sql):
    import pymysql
    coon = pymysql.connect(
        host='118.24.3.40', user='jxz', passwd='123456',
        port=3306, db='jxz', charset='utf8')
    cur = coon.cursor() #建立游标
    cur.execute(sql)#执行sql
    if sql.strip()[:6].upper()=='SELECT':
        res =  cur.fetchall()
    else:
        coon.commit()
        res = 'ok'
    cur.close()
    coon.close()
    return res

def my_md5(str):
    import hashlib
    new_str = str.encode() #把字符串转成bytes类型
    # new_str = b'%s'%str  #把字符串转成bytes类型
    m = hashlib.md5()  #实例化md5对象
    m.update(new_str)  #加密
    return m.hexdigest()  #获取结果返回

def reg():
    username = input('username:').strip()
    pwd = input('pwd:').strip()
    cpwd = input('cpwd:').strip()
    if username and pwd and cpwd:
        sql = 'select * from nhy where name="%s";'%username
        # select * from nhy where name='nhy';
        res = my_db(sql)
        if res:
            print('该用户已经存在!')
        else:
            if pwd == cpwd:
                md5_pwd = my_md5(pwd)
                insert_sql = 'insert into nhy (name,pwd) value ("%s","%s");'%(username,md5_pwd)
                my_db(insert_sql)
                print('注册成功!')
            else:
                print('两次输入的密码不一致')
    else:
        print('必填项不能为空!')

def login():
    username = input('username:').strip()
    pwd = input('pwd:').strip()
    if username and pwd:
        md5_pwd = my_md5(pwd)
        sql = 'select * from nhy where name="%s" and pwd="%s";'%(username,md5_pwd)
        # select * from nhy where name='nhy';
        res = my_db(sql)
        if res:
            print('欢迎,登录成功!今天是%s'%datetime.date.today())
        else:
            print('账号/密码错误!')
    else:
        print('必填项不能为空!')
login()

 

posted @ 2018-06-13 13:25  xiaomeng15  阅读(136)  评论(0编辑  收藏  举报