答:
# 根据输入的文件名进行复制新的文件名
old_file_name = input("请输入要复制的文件名:")
new_file_name = old_file_name[:old_file_name.rfind(".")] + "_copyfile" + old_file_name[old_file_name.rfind("."):]
# 以可读权限打开旧文件
old_file = open(old_file_name, "r")
# 以可写权限打开新文件
new_file = open(new_file_name, "w")
while True:
# 每次读1mb字节
str1 = old_file.read(1024 * 1024)
# 假如读的1mb字节的长度是零,跳出循环
if len(str1) == 0:
break
# 不为零,则写进新文件
else:
new_file.write(str1)
文件版本的学生管理系统如何实现(将数据保存在txt文件中)
答:
info_list = []
userName = "admin"
passWord = "123456"
def write_file():
# 注意读取格式
student_file = open("student.txt", "w", encoding="utf-8")
# 写入的必须是字符串,不能是数组
student_file.write(str(info_list))
student_file.close()
print("保存成功")
def read_file():
global info_list
# 注意读取格式
student_file = open("student.txt", "r", encoding="utf-8")
con = student_file.read()
# 写进去的时候是字符串,所以要去掉引号拿出来
info_list = eval(con)
student_file.close()
def welcome():
# 1、界面
print("--------学生信息管理器---------")
print("-----1、添加名片-----")
print("-----2、删除名片-----")
print("-----3、修改名片-----")
print("-----4、查询名片-----")
print("-----5、查询所有名片-")
print("-----6、保存进文件-----")
print("-----7、退出循环-----")
print("-" * 30)
def add_info():
name = input("请输入姓名:")
age = input("请输入年龄:")
sex = input("请输入性别:")
info_list.append({"name": name, "age": age, "sex": sex})
print("【info】:添加成功")
def remove_info():
name = input("删除:请输入姓名:")
for i in info_list:
if name in i.values():
info_list.remove(i)
print("【info】:删除成功")
break
else:
print("【Error】:查无此人")
def modify_info():
# 修改学生信息
print(info_list)
name = input("修改:请输入姓名:")
for i in info_list:
if name in i.values():
age = input("请输入修改后的年龄:")
sex = input("请输入修改后的性别:")
info_list[info_list.index(i)] = {"name": name, "age": age, "sex": sex}
print("【info】:修改成功")
break
else:
print("【Error】:查无此人")
def find_info():
userName_input = input("请输入你的用户名:")
passWord_input = input("请输入你的密码:")
if (userName == userName_input) and (passWord == passWord_input):
name = input("查找:请输入查找姓名:")
for i in info_list:
if name in i.values():
print("%s的信息:" % name)
for k, v in i.items():
print("%s : %s" % (k, v))
break
else:
print("【Error】:查无此人")
else:
print("【Error】:用户名或密码错误")
def findAll_info():
userName_input = input("请输入你的用户名:")
passWord_input = input("请输入你的密码:")
if (userName == userName_input) and (passWord == passWord_input):
print("所有学生信息如下:")
for i in info_list:
print(i)
else:
print("【Error】:用户名或密码错误")
def main():
read_file()
while True:
welcome()
# 2、输入,接收用户输入的数字,执行相应的操作
command = int(input("请输入你的操作:"))
# 3、通过判断用户输入的数字是1,还是2,还是3...执行相应操作
if command == 1:
# 添加学生信息
add_info()
elif command == 2:
# 删除学生信息
remove_info()
elif command == 3:
# 修改学生信息
modify_info()
elif command == 4:
# 查询学生信息
find_info()
elif command == 5:
# 查询所有学生信息
findAll_info()
elif command == 7:
# 退出系统
# print("退出系统成功,谢谢使用")
# break
sign = input("【info】:确定要退出吗?(yes or no):")
if sign == "yes":
print("退出系统成功,谢谢使用")
break
else:
print("返回到主页面")
continue
elif command == 6:
write_file()
else:
print("【Error】:请输入1-7之间的整数")
main() # 程序入口函数