1、打开文件
fi le_object = open(file_name, access_mode='r')
2.相对路径
open('./file1.txt') #打开当前目录的文件
open('./fold1/file2.txt') #打开当前目录fold1下的文件file2
open('../fold1/file3.txt') #打开当前目录的上级目录里的文件file3
3.只读文件 r
tell()文件指针的位置 read()读取文件的内容
seek()文件指针的移动 readlines()读取所有行
fh = open('file.txt')
lines = fh.readlines()
lines = fh.read().splitlines()
4.写文件 w
f = open('file.txt','w')
f1= f.write('abcdef')
f.flush() # 更新文件内容
5.读写文件 a (文件末尾追加内容)
f = open('file.txt','a')
6.文件打开的另一种写法
with open('file.txt','r') as f:
fc = f.read()
Python(3)文件的读写
最新推荐文章于 2024-09-11 18:01:31 发布