首页 > 代码库 > 输入打开文件

输入打开文件

import fnmatch, os
import re

yy = input(‘‘)
def edd(user_name):
    def allFiles(root, patterns = ‘*‘, single_level = False, yield_folders = False):
        patterns = patterns.split(‘;‘)
        for path, subdirs, files in os.walk(root):
            if yield_folders:
               files.extend(subdirs)
            files.sort()
            for name in files:
                for pattern in patterns:
                    if fnmatch.fnmatch(name, pattern):
                        yield os.path.join(path, name)
                        break
            if single_level:
                break
    for name in allFiles(‘D:\E\semantic\zzzz‘, single_level = True):
        w = re.findall(r‘\\([\w]+)\.txt‘,name)
        for i in w:
            if user_name == i:
                return i
rr = edd(yy)
print(rr)
f = open(‘D:\E\semantic\zzzz/%s.txt‘%rr,‘r‘)
print(f.read())

  

 pickle 版本购物车

import fnmatch, os
import re
import pickle

#写入注册信息
def inmation(user,pwd,phone,balance=0):
    name_user = {‘name‘:user,‘pwd‘:pwd,‘phone‘:phone,‘balance‘:balance}
    f = open(‘D:\E\semantic\zzzz/%s.txt‘%user,‘ab+‘)
    f.write(pickle.dumps(name_user))

#验证账号,在就返回True 不再返回False
def edd(user_name):
    def allFiles(root, patterns = ‘*‘, single_level = False, yield_folders = False):
        patterns = patterns.split(‘;‘)
        for path, subdirs, files in os.walk(root):
            if yield_folders:
               files.extend(subdirs)
            files.sort()
            for name in files:
                for pattern in patterns:
                    if fnmatch.fnmatch(name, pattern):
                        yield os.path.join(path, name)
                        break
            if single_level:
                break
    for name in allFiles(‘D:\E\semantic\zzzz‘, single_level = True):
        w = re.findall(r‘\\([\w]+)\.txt‘,name)
        for i in w:
            if user_name == i:
                # f = open(‘D:\E\semantic\zzzz/%s.txt‘%user_name,‘r‘)
                # t = f.read()
                return True
    return False

#验证账号密码
def user_pwd(user,pwd):
    f = open(‘D:\E\semantic\zzzz/%s.txt‘%user,‘rb‘)
    r = pickle.loads(f.read())
    print(r[‘name‘])
    if r[‘name‘] == user and r[‘pwd‘] == pwd :
        return True
    return False

#修改密码
def pwd_new(user,pwd,):
    f = open(‘D:\E\semantic\zzzz/%s.txt‘%user,‘rb+‘)
    r = pickle.loads(f.read())
    print(r[‘name‘])
    if r[‘name‘] == user and r[‘pwd‘] == pwd :
        while True:
            inp_new1 = input(‘输入新密码‘)
            inp_new2 = input(‘输入新密码‘)
            if inp_new1 == inp_new2:
                r[‘pwd‘] = inp_new2
                filename = ‘D:\E\semantic\zzzz/%s.txt‘%user
                f.close()
                os.remove(filename)
                f = open(‘D:\E\semantic\zzzz/%s.txt‘%user,‘ab+‘)
                f.write(pickle.dumps(r))
                return True
    return False

#主
def sgin():
    inp_1 = input(‘*******************************\n登录请按1,注册请按2,修改密码3\n*******************************\n‘)

    #账户登录
    if inp_1 == ‘1‘:
        while True:
            inp_user = input(‘请输入登录账号:‘)
            if edd(inp_user):
                print(‘账号可用‘)
            else:
                print(‘账户信息不存在‘)
                continue
            inp_pwd = input(‘请输入登录密码:‘)
            if user_pwd(inp_user,inp_pwd):
                print(‘恭喜登录成功‘)
                break
            else:
                print(‘密码输入错误‘)
    #账户注册
    elif inp_1 == ‘2‘:
        while True:
            inp_user = input(‘请输入注册登录账号:‘)
            user_new = edd(inp_user)
            if user_new:
                print(‘账号已经存在‘)
                continue
            inp_phone = input(‘请输入手机号号:‘)
            inp_pwd = input(‘请输入注册密码:‘)
            inmation(inp_user,inp_pwd,inp_phone)
            print(‘恭喜注册成功‘)
            sgin()
            break

    elif inp_1 == ‘3‘:
        while True:
            inp_user = input(‘请输入登录账号:‘)
            if edd(inp_user):
                print(‘账号可用‘)
            else:
                print(‘账户信息不存在‘)
                continue
            inp_pwd = input(‘请输入登录密码:‘)
            if pwd_new(inp_user,inp_pwd):
                print(‘恭喜修改成功‘)
                sgin()
                break
            else:
                print(‘密码输入错误‘)
sgin()

  

输入打开文件