'''
文件:
文件操作:
open()
path,filename
path:
绝对路径 : r'c:/cccc/cccc'
相对路径: 访问当前文件的路径,返回上层目录:
mode
读: r rb
写 w wb
stream = open(file,mode)
stream.read
stream.write
stream.close
whth open(file.mode) as XXXX
XXXXX.read....
os模块:
os.path常用函数:
dirname() 获取指定文件的目录
join()【拼接获取一个新的目录
split()分割 文件的目录和文件名字
splittext()分割 文件的目录和文件的类型
getsize()得到文件的大小(字节)
isabs()判断是否是绝对路径
isfile()判断是否是文件
isdir()判断是否是文件夹
os的常用函数:
os.getcwd()获得当前目录
os.listdir()游览文件夹返回一个列表
os.mkdir() 创建文件夹
so.rmdir() 删除空的文件夹
os.chdir()切换目录
'''
# 图书管理系统 持久化保存:文件
# 用户注册
def register():
usrname = input('输入用户名')
password = input('输入密码')
repassword = input('请确认密码')
if repassword == password:
# 保存信息
with open(r'E:untitledooksusers.txt', 'a') as wstream:
wstream.write('{} {}
'.format(usrname, password))
print('用户注册成功')
return True
else:
print('注册失败')
# 登录功能
def login():
global usrname
usrname = input('输入你的用户名')
password = input('输入密码')
if usrname and password:
with open(r'E:untitledooksusers.txt') as rstream:
while True:
user = rstream.readline()
if not user:
print('用户名或者密码错误')
return False
break
input_usrname = '{} {}
'.format(usrname, password)
if user == input_usrname:
print('用户登录成功')
return True
break
# 图书馆内有的书
def show_books():
print('----------------------')
with open(r'E:untitledooksooks.txt', 'r', errors='ignore', encoding='UTF-8') as rstream:
books = rstream.readlines()
for book in books:
print(book, end='')
# 借书
def Borrow_books():
print('欢迎进入电子图书馆')
print('1---------注册')
print('2---------登录后借书')
print('3---------看图书馆有哪些书')
choose = input('输入你的选择')
if choose == '1':
register()
elif choose == '2':
if login() == True:
borrow_book = input('输入你要借的书')
borrow_book = borrow_book+'
'
with open(r'E:untitledooksooks.txt', 'r', errors='ignore', encoding='UTF-8') as rstream:
books = rstream.readlines()
for book in books:
if borrow_book == book:
print('这一本书可以借,恭喜你')
borrow_book = borrow_book.replace('
', '')
with open(r'E:untitledooksusr_books.txt', 'a', encoding='UTF-8') as borrowstream:
borrowstream.write('{}:{}
'.format(borrow_book,usrname))
break
else:
print('稍等')
elif choose =='3':
show_books()
else:
print('你的选择错误')
#增加新书
def new_books():
new_book = input('输入要新加入的书名')
with open(r'E:untitledooksooks.txt','a') as wstream:
wstream.write('{}'.format(new_book))
print('录入成功')