目此文档分享如果python对文件的操作
python2.*打开文件:
file_obj = file("文件路径","模式") file_obj = open("文件路径","模式")Python3.*打开文件:
file_obj = open("文件路径","模式")打开文件的模式有:
r:以只读方式打开文件
w:打开一个文件只用于写入。如果该文件已存在则将其覆盖。如果该文件不存在,创建新文件
a:打开一个文件用于追加。如果该文件已存在,文件指针将会放在文件结尾。
w+:打开一个文件用于读写。如果该文件已存在则将期覆盖。如果该文件不存在,创建新文件。
例:
#写文件 ''' f = open("text.log","w") f.write("this is the first line!\n") f.write("this is the 3 lien!\n") f.write("this is the 4 lien!\n") f.close() #读文件 f = open("text.log","r") #print(f.read()) print(f.readlines()) f.close() #追加文件 f = open("text.log","a") f.write("6\n") f.write("7\n") f.close() ''' #读写文件 f = open("text.log","w+") f.write("new line\n") print(f.read()) f.close()
实例:
------说明---- 文件介绍: start.py 为程序启动主文件 marry.py 为验证用户是否存在文件 keys.py 为是否解除用户锁定文件 info.txt 为锁用户的文件库 1\运行程序start.py 2\输入用户名和密码(输错密码3次会被锁) username:tale password:123456 3\用户验证 1)用户是否被锁 2)是否要解锁该用户 3)验证输入的用户名和密码是否正确 4)解锁用户需要输入正确的用户名和密码 4\三级菜单 1)产品列表 2)当单选中产品,会显示产品价格信息 start.py代码
#!/usr/bin/env python # -*- coding:utf-8 -*- from marry import file_bool file_name = open("info.txt","r+") file_list = file_name.readlines() userinfos = ["tale","123456","10000","1"] Shopping = {"Commodity":["1:Cup","2:Young lady","3:AV"]} Price = {"1":"10","2":"888","3":"666"} username = userinfos[0] passwords = userinfos[1] stop = int(userinfos[3]) #user login for i in range(3): user = raw_input("input username:").strip() pa = raw_input("input password:") #判断用户是否存在 if user == userinfos[0]: pass else: print("not username!") exit() #判断用户是否已被锁定 file_bool(user,pa) if user == username and pa == passwords: print("welcome %s login!" % user) break else: print("password or username err!") stop += 1 userinfos[3] = stop if userinfos[3] == 4: print("The user is locked.") lst_info = str(userinfos) values_list = "".join(lst_info) file_name.write(values_list+"\n") file_name.close() exit() else: pass #三级菜单 while True: for comm,valus in Shopping.items(): print(valus) Shopgo =raw_input("是否要购买 n/y:") if Shopgo == "n" or Shopgo == "N": exit() elif Shopgo == "y" or Shopgo == "Y": continue else: print("不按规定输入,用户强制退出!") exit() print("Price tabulation") for key,sp in Price.items(): print("ID:%s Price:%s$" % (key,sp)) IDcode = raw_input("Please input ID:") if IDcode == "1": print("Success purchase commodity:Cup") elif IDcode == "2": print("Success purchase commodity:Young lady") elif IDcode == "3": print("Success purchase commodity:AV") else: print("Not this commodity!") exit() marry.py代码
#!/usr/bin/env python # -*- coding:utf-8 -*- from keys import keyinfo def file_bool(users,passinfo): file_name = open("info.txt","r+") file_list = file_name.readlines() for ele in file_list: line = ele.strip() values_list = line.split(',') last_value = str(values_list[-1]) last_user = str(values_list[0]) last_pass = str(values_list[1]) strip_pass = last_pass.strip(" ") strip_pass2 = strip_pass.strip("'") strip1 = last_user.strip("[") strip2 = strip1.strip("'") if strip2 == users and strip_pass2 == passinfo: if last_value[1] >= 4 and strip2 == users: keys = raw_input("是否要解锁%s用户:Y/N" % users) if keys == "Y" or keys == "y": keyinfo() print("用户已解锁!") exit() elif keys == "N" or keys == "n": print("程序退出!") exit() else: print("请输入正确选项!") exit() else: pass file_name.close() else: print("用户被锁,程序退出.请输入正确的用户名和密码解锁!") exit() keys.py代码
#!/usr/bin/evn python def keyinfo(): file_name = open("info.txt","r+") file_name.truncate() file_name.close()