首页 技术 正文
技术 2022年11月15日
0 收藏 625 点赞 3,898 浏览 2592 个字

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\n")
f.write("hello world2\n")
f.write("hello world3\n")
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("\n追加2")
f.close()f = open("test.txt","r",encoding="utf-8")
data = f.read()
print(data)
f.close()执行结果
E:\Users\xiajinqi\PycharmProjects\twoday\venv\Scripts\python.exe E:/Users/xiajinqi/PycharmProjects/twoday/file.py
hello world1
hello world2
hello world3hello world1
hello world2
hello world3
追加1
追加2Process finished with exit code 0

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

 # Author : xiajinqi
# 文件读按照行数读
#方式一,读全部内容(文件太大时候,内存会爆掉)
f = open("test.txt","r",encoding="utf-8")
print("一次性读出来--------------")
print(f.read())
f.close() #方式2,一行一行读readline ,读出所有的行,并且转换为数组f.readlines()
#由于一直在往内存读,导致内存会爆掉,这种循环又称为low
f = open("test.txt","r",encoding="utf-8")
print("一行一行读low looper--------------")
for key,line in enumerate(f.readlines()):
print(key,line.strip())
f.close() # 方式3 :文件循环读。内存每次只有一行,读一行,关闭一行,内存永远不会爆掉。建议使用3,效率最高
f = open("test.txt","r",encoding="utf-8")
print("一行一行读bigger looper--------------")
for line in f :
print(line.strip())
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)
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,904
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,430
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,247
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,058
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,690
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,727