1.方法一(不建议使用)
file=open("./data.txt",'r')
file.close()
open(filename, mode)
filename
:文件路径,绝对路径相对路径均可
mode
:打开方式,mode 参数是可选的,省略时默认为'r'
'r'
:表示文件只能读取
'w'
:表示只能写入(已存在的同名文件会被删除)
'a'
:表示打开文件以追加内容,写入的数据会自动添加到文件的末尾
'r+'
:表示打开文件进行读写
文件默认是以 text mode 打开的,这意味着从文件中读取或写入字符串时,都会以指定的编码方式进行编码。
在mode 中追加的'b'
则以 binary mode 打开文件
2.方法二
with 后的变量会求值,返回值赋给as后的变量
不需要关闭文件,会自动关闭
如果没有使用 with 关键字,那么应该调用 f.close() 来关闭文件并立即释放它使用的所有系统资源
with open("./data.txt","r") as file:
file.read()
要读取文件内容,调用file.read(size)
,它会读取一些数据并将其作为字符串(在文本模式下)或字节串对象(在二进制模式下)返回。
size
是一个可选的数值参数:
当 size 被省略或者为负数时,将读取并返回整个文件的内容;
当size为其他数时,读取字节与机器字长有关,此处不再赘述
如果已到达文件末尾,f.read() 将返回一个空字符串 (’’)。
with open("./data.txt","r") as file:
a=file.read()
print(a)
----------------------------------------
1 532
3 321
4 312
5 132
等同于:
file=open("./data.txt",'r')
a=file.read()
print(a)
file.close()
- 读取
1.file.readline()
从文件中读取一行;
换行符\n
留在字符串的末尾,如果文件不以换行符结尾,则在文件的最后一行省略。
如果 f.readline() 返回一个空的字符串,则表示已经到达了文件末尾,而空行使用\n
表示,该字符串只包含一个换行符。
在文本模式下读取时,默认会把平台特定的行结束符
(Unix 上的 \n, Windows 上的 \r\n) 转换为 \n
换行符就是另起一新行,光标在新行的开头
回车符就是光标回到一旧行的开头;(即光标目前所在的行为旧行)
在文本模式下写入时,默认会把出现的 \n 转换回平台特定的结束符
file=open("./test.txt","r")
a=file.readline()
print(a)
a=file.readline()
print(a)
file.close()
--------------------------------
1 23
4 51
等同于:
with open("./data.txt","r") as file:
a=file.readline()
print(a)
a=file.readline()
print(a)
等同于:
file=open("./data.txt","r")
for row in file:
print(row)
file.close()
等同于:
with open("./data.txt","r") as file:
for row in file:
print(row)
2.以列表的形式读取文件中的所有行
file.readlines()
等价于list(file)
file=open("./data.txt","r")
a=file.readlines()
print(a)
file.close()
-------------------------------------
['1 532\n', '3 321\n', '4 312\n', '5 132']
等价于:
file=open("./data.txt","r")
b=list(file)
print(b)
file.close()
等价于:
with open("./data.txt","r") as file:
a=file.readlines()
print(a)
等价于:
with open("./data.txt","r") as file:
b=list(file)
print(b)
- 关于文件对象位置
**file.tell()
**返回一个整数,给出文件对象在文件中的当前位置:
表示为二进制模式下时从文件开始的字节数,以及文本模式下的意义不明的数字。
file=open("./data.txt",'r')
pos=file.tell()
print(pos)
row=file.readline()
print(row,end="")
pos=file.tell()
print(pos)
row=file.readline()
print(row,end="")
pos=file.tell()
print(pos)
--------------------------------
0
1 23
6 #'1',' ','2','3','/','n'
4 51
13
**file.seek(offset, whence)
**改变文件对象的位置
offset 参考点来计算,其由 whence 参数指定
whence 的 0 值表示从文件开头起算,1 表示使用当前文件位置,2 表示使用文件末尾作为参考点。
whence 如果省略则默认值为 0,即使用文件开头作为参考点。
file.seek(6,0)
pos=file.tell()
print(pos)
row=file.readline()
print(row,end="")
file.close()
3.写文件
写文件时,一定要注意文件权限为"w"或"a"或"r+"
写文件file.write(string)
会把 string 的内容写入到文件中,并返回写入的字符数
with open("./data.txt","w") as file:
file.write("123\n")
a=[1,2,3,4,5]
file.write(a)
4.json
import json
将一个数据对象转为json字符串格式:
data = {
'name' : 'myname',
'age' : 100,
}
json_str = json.dumps(data)
print(json_str)
--------------------------------
'{"name": "myname", "age": 100}'
将json字符串格式存到json文件中:
with open('test.json', 'w') as f:
json.dump(data, f)
将json文件中的内容读出:
with open("test.json","r") as f:
data=json.load(f)
print(data)