1.文件备份
用户自己输入想要备份的文件,及备份保存的路径(必须以/结尾)。备份文件名称为“当前用户名+保存时间+后缀”
#coding=utf-8
import os
import time
back_file =[]
a=1
while 1:
filename = raw_input("请输入你所要备份的文件名称(含绝对路径)quit为退出:")
if filename == 'quit':
break
else:
while 1:
if os.path.isfile(filename):
back_file.append(filename)
break
else:
print "%s文件不存在" %filename
break
while 1:
dstBackDir = raw_input("请输入存储备份的目录:")
if os.path.isdir(dstBackDir):
break
else:
print "目录不存在"
userlogin = os.getlogin()
datename = time.strftime("%Y%m%d%H%M")
comond = "tar zcf %s %s" %(dstBackDir+userlogin+datename+"back.tar.gz"," ".join(back_file))
if os.system(comond) == 0:
print "文件备份成功!"
else:
print "文件备份失败!"
2.用户登录
使用数据库对用户信息存储,作者是在自己的linux环境下,先安装数据库及相应python环境。代码功能包括新建用户,用户登陆,删除用户,显示用户。
##########################
655 yum install mariadb-server.x86_64 -y
656 systemctl start mariadb.service
658 mysql_secure_installation
659 yum search mysql |grep python
660 yum install MySQL-python.x86_64 -y
##############
#coding=utf-8
import MySQLdb
import time
def useradd():
while 1:
username = raw_input("请输入注册用户名:")
passwd = raw_input("用户注册密码:")
conn = MySQLdb.connect(user='root',passwd='1234',db='userlogin',host='localhost',charset='utf8')
cur = conn.cursor()
cur.execute('select * from userinfo where username="%s";' %username)
if len(cur.fetchall()) == 0:
logintime = '0000'
cur.execute('insert into userinfo value("%s","%s","%s");'%(username,passwd,logintime))
conn.commit()
cur.close()
conn.close()
print "用户创建成功"
break
else:
print "该用户名存在"
def login():
username = raw_input("用户名:")
passwd = raw_input("密码:")
conn = MySQLdb.connect(user='root',passwd='1234',db='userlogin',host='localhost',charset='utf8')
cur = conn.cursor()
cur.execute('select * from userinfo where username="%s" and passwd="%s";' %(username,passwd))
if len(cur.fetchall()) == 0:
print "用户名或密码错误"
else:
print "登陆成功"
cur.scroll(0,"absolute")
b = cur.fetchall()
e = int(b[0][2])
if e == 0:
print "欢迎新用户!"
nowtime = time.strftime("%Y%m%d%H")
cur.execute('update userinfo set logintime="%s" where username="%s";' %(nowtime,username))
conn.commit()
else:
nowtime = time.strftime("%Y%m%d%H")
f = int(time.strftime("%Y%m%d%H"))
if f-e >4:
print "%s用户距上次登陆超过4小时!" %username
else:
print "%s用户上次登陆时间为%s" %(username,e)
cur.execute('update userinfo set logintime="%s" where username="%s";' %(nowtime,username))
cur.close()
conn.close()
def dele():
username = raw_input("请输入要删除的用户名:")
conn = MySQLdb.connect(user='root',passwd='1234',db='userlogin',host='localhost',charset='utf8')
cur = conn.cursor()
cur.execute('select * from userinfo where username="%s";' %username)
if len(cur.fetchall()) == 0:
print "用户不存在,请确认信息"
else:
cur.execute('delete from userinfo where username="%s";' %username)
conn.commit()
print "%s用户删除成功" %username
cur.close()
conn.close()
def show():
conn = MySQLdb.connect(user='root',passwd='1234',db='userlogin',host='localhost',charset='utf8')
cur = conn.cursor()
cur.execute("select * from userinfo;")
a = cur.fetchall()
for i in a:
print i
cur.close()
conn.close()
def menu():
while 1:
print '''
(A)dduser
(L)ogin
(D)elect
(S)how
(Q)uit
'''
choice = raw_input("选择:")
if choice == 'A' or choice == 'a':
useradd()
break
elif choice == 'L' or choice == 'l':
login()
elif choice == 'D' or choice == 'd':
dele()
elif choice == 'S' or choice == 's':
show()
elif choice == 'Q' or choice == 'q':
break
else:
print "error"
menu()