day6_python课后习题

2018-12-10 23:15:37

作业:
1、写一个函数,传入一个路径和一个关键字(关键字是文件内容),找到文件内容里面有这个关键字的文件
2、写一个清理日志的程序,把三天前的日志删掉,
          保留今天的、昨天和前天的。
3、写一个注册的功能,要求数据存在数据库里面
          1)、名字为空、已经存在都要校验
          2)、校验通过之后,密码要存成密文的。
4、登陆的功能
          登录的账号密码从数据库里面取,
          如果输入用户不存在要提示

代码1:

 1 #_author:'ZYB'
 2 #data:2018/12/9
 3 import os
 4 def find_file(keywords,path='D:HomeWorkHomeWorkday6'):
 5     file_list = get_all_file(path)
 6     for file in file_list:
 7         if file.endswith('.py') or file.endswith('.txt'):
 8             with open(file,'r',encoding='utf-8') as fr:
 9                 if keywords in fr.read():
10                     print(file)
11                     print('=='*20)
12 def get_all_file(path):
13     file_list = []
14     for cur_path, cur_dirs, cur_files in os.walk(path):
15         for name in cur_files:
16             file_list.append(os.path.join(cur_path,name))
17     return file_list
18 keywords = input("Please input the key words that you want to search:")
19 path = input("Please input the filepath:")
20 find_file(keywords,path)
View Code

 代码2:

 1 #_author:'ZYB'
 2 #data:2018/12/9
 3 import os
 4 import time
 5 def Clear_logs(N):
 6     file_list = get_all_file()
 7     for file in file_list:
 8         if file.endswith('.log'):
 9             f = os.path.split(file)[1]
10             t = f[-14:-4]
11             if time.time()-StrToTimestamp(t) >= 24*60*60*int(N):
12                 os.remove(file)
13 def get_all_file(path='D:HomeWorkHomeWorkday6logs'):
14     file_list = []
15     for cur_path, cur_dirs, cur_files in os.walk(path):
16         for name in cur_files:
17             file_list.append(os.path.join(cur_path,name))
18     return file_list
19 def StrToTimestamp(Str=None,format='%Y-%m-%d'):
20     #格式化好的时间转时间戳:
21     if Str:
22         timep = time.strptime(Str, format)
23         res = time.mktime(timep)
24     else:
25         res = time.time()
26     return int(res)
27 N = input('请输入需要清除几天前的日志:')
28 Clear_logs(N) #清除N天前的日志
View Code

代码3:

 1 #_author:'ZYB'
 2 #data:2018/12/10
 3 import pymysql
 4 import hashlib
 5 def CheckUserInSql(user):
 6     conn = pymysql.connect(host='118.24.3.40',user='jxz',
 7                     password='123456',port=3306,db='jxz',charset='utf8')
 8     cur = conn.cursor(pymysql.cursors.DictCursor) #加了这个参数,返回值是字典形式的元组
 9     sql = 'select * from app_myuser where username="%s";' %user # 查看数据库是否以及存在user用户
10     cur.execute(sql) #只是执行sql,并不会返回数据
11     res = cur.fetchall()
12     cur.close()  # 关闭游标
13     conn.close()  # 关闭连接数据库
14     if len(res) == 0:
15         return True
16     else:
17         return False
18 
19 def isNotNone(user):
20     if str(user).strip()=='':
21         return False
22     else:
23         return True
24 def StoreInSQL(user,pwd,admin):
25     conn = pymysql.connect(host='118.24.3.40', user='jxz',
26                            password='123456', port=3306, db='jxz', charset='utf8')
27     cur = conn.cursor(pymysql.cursors.DictCursor)  # 加了这个参数,返回值是字典形式的元组
28     sql = 'insert into app_myuser (username,passwd,is_admin) values ("%s","%s",%d);'%(user,pwd,admin)
29     cur.execute(sql)
30     conn.commit()
31     cur.close()
32     conn.close()
33 def my_md5(s):
34     news = str(s).encode()
35     m = hashlib.md5(news)
36     return m.hexdigest()
37 
38 user = input('请输入注册的用户名:')
39 pwd = input('请输入注册密码:')
40 is_admin = int(input('请输入注册账号的权限(0非管理员/1管理员):'))
41 if isNotNone(user):
42     if CheckUserInSql(user):
43         pwdmd5 = my_md5(pwd)
44         StoreInSQL(user,pwdmd5,is_admin)
45     else:
46         print('输入的账号已经存在!')
47 else:
48     print('输入的账号不能为空!')
View Code

代码4:

 1 #_author:'ZYB'
 2 #data:2018/12/10
 3 import pymysql
 4 import hashlib
 5 def CheckUserInSql(user):
 6     conn = pymysql.connect(host='118.24.3.40',user='jxz',
 7                     password='123456',port=3306,db='jxz',charset='utf8')
 8     cur = conn.cursor(pymysql.cursors.DictCursor) #加了这个参数,返回值是字典形式的元组
 9     sql = 'select * from app_myuser where username="%s";' %user # 查看数据库是否以及存在user用户
10     cur.execute(sql) #只是执行sql,并不会返回数据
11     res = cur.fetchall()
12     cur.close()  # 关闭游标
13     conn.close()  # 关闭连接数据库
14     if len(res) == 0:
15         return True, res
16     else:
17         return False, res
18 def my_md5(s):
19     news = str(s).encode()
20     m = hashlib.md5(news)
21     return m.hexdigest()
22 
23 user = input('请输入登录账号:')
24 pwd = input('请输入登录密码:')
25 pwdmd5 = my_md5(pwd)
26 Condition, res = CheckUserInSql(user)
27 if not Condition:
28     if res[0]['passwd'] == pwdmd5:
29         print('恭喜%s登录成功!'%user)
30     else:
31         print('密码错误!')
32 else:
33     print('账号不存在!')
View Code
原文地址:https://www.cnblogs.com/arraon/p/10099899.html