文件读取等操作

模式描述
“r”为了读取打开一个文件
“w”为了写入打开一个文件,如果文件已存在,他的内容就被销毁
“a”打开一个文件从文件末尾追加数据
“rb”为读取二进制数据打开文件
“wb”为写入二进制数据打开文件

例子

#myfile文件,open函数,第一个参数路径,第二个参数读取
myfile = open("C:\\xxx\xxx\\1.txt","r")
mylist = myfile.readlines()读取每一行到每一个列表
print(myfile)#对象
print(mylist)#数据

在这里插入图片描述

myfile = open("C:\\xxx\xxx\\1.txt","r")
print(myfile.read(3))#没有参数,读取全部,有参数为n,读取n个字符

结果:132 参考上一张图

#myfile文件,open函数,第一个参数路径,第二个参数读取
myfile = open("C:\\xxx\xxx\\1.txt","r")
print(myfile.readline())#读取一行包含换行符
print(myfile.readline())
print(myfile.readline())

结果:1321321

        撒旦撒
  
   啊水水倒萨

#myfile文件,open函数,第一个参数路径,第二个参数读取
myfile = open("C:\\xxx\xxx\\1.txt","r")
print(myfile.readline(),end="")#读取一行包含换行符
print(myfile.readline(),end="")
print(myfile.readline(),end="")

结果:1321321
   撒旦撒
   啊水水倒萨

例子:如何查开房信息

myfile = open("C:\\xxx\xxx\\1.txt","r",error="ignore")#error解决解码jbk的问题
mylist = myfile.readlines()#读取每一行到每一个表
for line in mylist:#遍历列表
   if line.find("王涛")!=-1:
   	print(line)

如果在上个例子中变成输入人名查找

myfile = open("C:\\xxx\xxx\\1.txt","r",error="ignore")
mylist = myfile.readlines()#读取每一行到每一个表
while True:
  mystr=input"我要查谁"
  for line in mylist:#遍历列表
  	if line.find(systr)!=-1:
  		print(line)

函数改造:

def load(path):
   myfile = open(path, "r", error="ignore")
   mylist = myfile.readlines()#读取每一行到每一个列表
   return mylist

def find(mylist,findstr):
   for line in mylist:
   	if line.find(findstr)!=-1
   		print(line)
mylist = load(r"C:\\xxx\xxx\\1.txt","r",error="ignore "")
while True:
   mystr = input("要查谁")
   find(mylist, mystr)

硬盘数据查询,减少内存消耗,上一个是内存查询

myfile = open(r"C:\\xxx\xxx\\1.txt","r",error="ignore")
findstr = input("我要查谁")
while True:
	mystr = myfile.readline()#读取一行,读不到返回None
	if mystr.find(findstr)!= -1:
		print(findstr)
	if not mystr:#读取不到跳出循环 not none=True
		break	

文件写入

w代表写入,重写,原来的文件会被覆盖
myfile = open(r"C:\\xxx\xxx\\1.txt","w")
myfile.write("12321321")
myfile.write("萨达苏")
myfile.close()#关闭

结果:1.txt文本会显示:12321321萨达苏 并没有换行
换行只需要:myfile.write(“12321321\n”)

close的作用

import time
w代表写入,重写,原来的文件会被覆盖
myfile = open(r"C:\\xxx\xxx\\1.txt","w")
myfile.write("12321321")
myfile.write("萨达苏")
time.sleep(30)#延迟30秒
myfile.close()#close,缓存区的内容在文件生效

会发现生成的1.txt并不会有内容写入,为什么呢?
没有close时,我们写入的write内容放入了缓存,而缓存不是实时写入的,比如说我们找班长买票,全去找他了,他就很烦,这时候我们一批一批的找他就会降低找他的次数,缓存也一样,比如输入凑够了10M,那么就进行一次输入,这样子就降低了硬盘的读写次数。
如果需要实时写入

myfile.flush()#立刻写入

查询保存数据

def load(path):
	myfile = open(path, "r", error="ignore")
	mylist = myfile.readlines()#读取每一行到每一个列表
	return mylist

def find(mylist,findstr):
	savefile=open("C:\\xxx\xxx\\1.txt"+findstr+".txt","w")
	for line in mylist:
		if line.find(findstr)!=-1
			print(line)
			savefile.write(line)#写入
	savefile.close()
mylist = load(r"C:\\xxx\xxx\\1.txt","r",error="ignore "")
while True:
	mystr = input("要查谁")
	find(mylist, mystr)

结果:以搜王毅为例
在这里插入图片描述

类数据查询并保存

class  Filefind:
    def  __init__(self,path):#构造
        self.path=path
        self.file = open(self.path, "r", errors="ignore")#打开文件
        self.mylist=[]
    def  memload(self):
        self.mylist=self.file.readlines()#载入文件到列表

    def memsearch(self, findstr):  # 在内存的list内部查找
        for line in self.mylist:
            if line.find(findstr) != -1:
                print(line)


    def  memsearchsave(self,findstr,savepath): #在内存的list内部查找
        savefile=open(savepath,"w") #写入
        for  line in self.mylist:
            if line.find(findstr)!=-1:
                print(line)
                savefile.write(line) #保存
        savefile.close()


    def  disksearch(self,findstr):
        while True:
            mystr=self.file.readline()
            if mystr.find(findstr)!=-1: #找到
                print(mystr)
            if not mystr:
                break

    def disksearchsave(self, findstr, savepath):
        savefile = open(savepath, "w")  # 写入
        while True:
            mystr = self.file.readline()
            if mystr.find(findstr) != -1:  # 找到
                print(mystr)
                savefile.write(mystr)  # 保存
            if not mystr:
                break
        savefile.close()

    def __del__(self):#销毁
        self.file.close()#关闭文件

filekf =Filefind(r"C:\Users\Tsinghua-yincheng\Desktop\yinchengDay1\kaifangX.txt")
filedd=Filefind(r"C:\Users\Tsinghua-yincheng\Desktop\yinchengDay1\dangdangwang.txt")

print("开始检索开房")
filekf.disksearchsave("王涛",r"C:\Users\Tsinghua-yincheng\Desktop\wangtaokf.txt")
print("开始检索购书")
filedd.disksearchsave("王涛",r"C:\Users\Tsinghua-yincheng\Desktop\wangtaodd.txt")



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

青灯有味是儿时

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值