python第三次作业

1.文件备份

用户自己输入想要备份的文件,及备份保存的路径(必须以/结尾)。备份文件名称为“当前用户名+保存时间+后缀”

#!/usr/bin/env python
#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

##############

#!/usr/bin/env python
#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()
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值