python读写文件相关内容

1。python读取文件

f = open('E:/info.txt', 'r')#用read的方式打开
a = 0
for line in f.readlines():读取没一行,就是读取所有文件的意思
getstr = line.split()#将一整行文件进行切割,切割后可以下标找到对应位置
if getstr[0] == user_name:#通过下表找到对应位置的信息并进行比较等操作。
print('用户名存在,请重新输入')

f.close()#关闭对应文件

2.python写文件,
f = open('E:/info.txt', 'a')#以append的方式,增加写文件,如果参数不是a,而是w,则表示每次都重写。
f.write('%s %s'%(user_name,password)+' ')#将内容写道对应的文件里。
print("成功")
f.close()#关闭文件
原文地址:https://www.cnblogs.com/xiaoshidi/p/6864705.html