python学习之文件读写入门(文件读的几种方式比较)

1、文件读写简单实例:(以w写的方式打开一个文件,以r读一个文件)

# Author : xiajinqi

# 文件读写的几种方式
# 文件读写
f = open("D://test.txt","w",encoding="utf-8")
f.write("hello world")
f.flush()
f.close()

f =   open("D://test.txt","r",encoding="utf-8")
data = f.read()
data2 = f.read()
f.seek(0,0)
data3 = f.read()
print("data-------------------",data)
print("data2------------------",data2)
print("data3-----------------",data3)

2、文件写的w和a 简单介绍

# Author : xiajinqi

# 文件读写的几种方式
# 文件写,文件写,如果就文件存在,会清空旧文件内容(切记),如果不存在就创建。并且不能读
f = open("test.txt","w",encoding="utf-8")
f.write("hello world1
")
f.write("hello world2
")
f.write("hello world3
")
f.flush()
f.close()

f =   open("D://test.txt","r",encoding="utf-8")
data = f.read()
print(data)
f.close()

# 文件追加,不能读,在文件尾部追加,不会清空旧的文件
f =  open("test.txt","a",encoding="utf-8")
f.write("追加1")
f.close()

f =  open("test.txt","a",encoding="utf-8")
f.write("
追加2")
f.close()

f =   open("test.txt","r",encoding="utf-8")
data = f.read()
print(data)
f.close()

执行结果
E:UsersxiajinqiPycharmProjects	wodayvenvScriptspython.exe E:/Users/xiajinqi/PycharmProjects/twoday/file.py
hello world1
hello world2
hello world3

hello world1
hello world2
hello world3
追加1
追加2

Process finished with exit code 0


3、文件读r的详细使用。文件读的几种方式和优缺点:

 1 # Author : xiajinqi
 2 # 文件读按照行数读
 3 #方式一,读全部内容(文件太大时候,内存会爆掉)
 4 f =  open("test.txt","r",encoding="utf-8")
 5 print("一次性读出来--------------")
 6 print(f.read())
 7 f.close()
 8 
 9 
10 #方式2,一行一行读readline ,读出所有的行,并且转换为数组f.readlines()
11 #由于一直在往内存读,导致内存会爆掉,这种循环又称为low
12 f = open("test.txt","r",encoding="utf-8")
13 print("一行一行读low looper--------------")
14 for key,line in  enumerate(f.readlines()):
15     print(key,line.strip())
16 f.close()
17 
18 
19 
20 # 方式3 :文件循环读。内存每次只有一行,读一行,关闭一行,内存永远不会爆掉。建议使用3,效率最高
21 f =  open("test.txt","r",encoding="utf-8")
22 print("一行一行读bigger looper--------------")
23 for line  in  f :
24     print(line.strip())
25 f.close()

4、练习题目 ,实现第九行不打印的两种方式

方式一:
f =  open("test.txt","r",encoding="utf-8")
print("不打印第九行")
count = 0
for line  in  f :
    count = count + 1
    if count == 9 :
         print("分隔符>>>>>>>>>>>>>>>>>>>")
         continue
    print(line.strip())
f.close()

方式二:
f =  open("test.txt","r",encoding="utf-8")
print("不打印第九行方式二")
for key,line  in  enumerate(f.readlines()) :
    if key == 8 :
         print("分隔符>>>>>>>>>>>>>>>>>>>")
         continue
    print(key+1,line.strip())
f.close()

5、seek(),tell()  介绍和总结:

#tell 记录当前指针的位置(字符位置),seek设置指针的位置

f =  open("test.txt","r",encoding="utf-8")
print("第一次打印")
print(f.read())  
print(f.read())  # 指针已经到了文件尾部,继续读将为空
print("第二次打印")
f.seek(0,0)
print(f.read())
f.close()

#查找当前位置
f =  open("test.txt","r",encoding="utf-8")
f.readline()
print(f.tell())
f.close()

 6、文件的其他函数总结:

#
f =  open("test.txt","r",encoding="utf-8")
print("其他函数使用")
print(f.fileno())  #文件在系统中的编号,一般
print(f.name)  #文件名字
print(f.seekable()) #终端设备无法移动
print(f.readable())  #文件是否可以读
f.close()

 7、flush 使用。将内存数据写到文件(系统默认不是实时刷新)

import sys,time
# 显示进度条
for i  in  range(10) :
    sys.stdout.write("#")
    sys.stdout.flush()
    time.sleep(0.2)
良禽择木而栖 贤臣择主而侍
原文地址:https://www.cnblogs.com/xiajq/p/8732818.html