python之文件操作2

1.x模式(控制文件操作的模式)–>了解

x模式,只写模式[不可读;不存在则创建,存在则报错]

with open(r'c.txt',mode='x',encoding='utf-8') as f:
	f.read() #报错

with open(r'x.txt',mode='x',encoding='utf-8') as f:
	f.write('哈哈哈哈哈哈') #指针将在头部

with open (r'd.txt',mode = 'x',encoding='utf-8') as f:
	f.write('哈哈哈哈哈哈\n') #指针将位于尾部

2.b模式
t模式:①读写都是以字符串(unicode)为单位的;②只能针对文本文件;③必须指定字符编码,即必须指定encoding参数
b模式:①读写都是以bytes为单位;②可以针对所有文件;③一定不能指定字符串编码,即一定不能指定encod编码
t/b模式总结:①在操作纯文本文件方面,t模式帮助我们省去了编码与解码的环节,b模式则需要解码的环节;②针对非文本文件(如图片、视频、音频等)只能使用b模式

d.txt的内容
你可真是个小机灵鬼!

test.jpg是一张图片

b.txt的内容
111
222
333
444
555
666

aaa.txt的内容
abc你好
要知道英文字符1个bytes,中文字符3个bytes
with open (r'test.jpg',mode = 'rb') as f:
	res = f.read() #硬盘的二进制读入内存-->b模式下,不做任何转换,直接读入内存
	print(res,type(res))
b模式下,读取d.txt
with open (r'd.txt',mode='rb') as f:
	res = f.read()
	print(res,type(res))
	print(res.decode('utf-8')
t模式下读取d.txt
with open (r'd.txt',mode = 'rt',encoding='utf-8') as f:
	res = f.read()
	print(res)
with open(r'e.txt',mode='wb') as f:
	f.write('你好hello'.encode('gbk'))

with open(r'f.txt',mode = 'wb') as f:
	f.write('你好hello'.encode('utf-8'))

文件拷贝工具升级版(各种类型文件)

src_file = input('源文件路径>>:').strip()
dst_file = input('源文件路径>>:').strip()
with open (r'{
    
    }'.format(src_file),mode='rb') as f1,\
	open (r'{
    
    }'.format(dst_file),mode='wb') as f2:
	#res = f1.read()  #可能导致内存占用过大
	#f2.write(res)
	for line in f1::
		f2.write(line)

#循环读取文件

#方式一:自己控制每次读取的数据的数据量
with open (r'test.jpg',mode='rb') as f:
	while True:
		res = f.read(1024) #按照字节
		if len(res) == 0:
			break
		print(len(res))
#方式二:以行为单位读,当一行内容过长时会导致一次性读入内容的数据量过大
with open (r'b.txt', mode = 'rt',encoding='utf-8') as f:
	for line in f:
		print(len(line),line,end='')

with open(r'b.txt',mode='rb') as f:
	for line in f:
		print(line)

with open(r'test.jpg',mode = 'rb') as f:
	for line in f:
		print(line)

一 读相关的
①readline:一次读一行

with open (r'b.txt',node='rt',encoding='utf-8') as f:
	res1 = f.readline()
	res2 = f.readline()
	print(res2)

	while True:
		line = f.readline()
		if len(line) == 0:
			break
		print(line,end='')

②readlines()

with open (r'b.txt',mode = 'rt',encoding='utf-8') as f:
	res = f.readlines()
	print(res)

二、写相关的
f.writelines():

with open (r'h.txt',mode='wt',encoding='utf-8') as f:
	#f.writelines('111\n222\n333\n'444) #报错,只能写入字符串
	l = ['111\n','222\n','333\n\]
	for line in l:
		f.write(line)
	f.wrutelines(l)
with open(r'h.txt',mode='wb') as f:
	l = ['111\n'.encode('utf-8'),
			'222'.encode('utf-8'),
			'333\n'.encode('utf-8')
		]
	f.writelines(l)

#补充1:如果是纯英文字符,可以直接加前缀b得到bytes类型
	l = [b'111aaa1\n',
			b'222bb2',
			b'333ccc33']
	f.writelines(l)

#补充2:‘上’.encode('utf-8')等同于bytes(‘上’.encoding=’utf-8')
	l = [
			bytes('上啊',encoding = 'utf-8'),
			bytes('冲啊',encoding = 'utf-8'),
			bytes('小垃圾们',encoding = 'utf-8'),
			]
	f.writelines(l)

#flush

with open(r'h.txt',mode = 'wt',encoding = 'utf-8') as f:
	f.write('哈哈哈哈哈')
	f.flush()
with open (r'h.txt',mode ='wt',encoding='utf-8') as f:
	print(f.readable())
	print(f.writable())
	print(f.encoding)
	print(f.name)
print(f.closed)

4.文件的高级操作:
控制文件指针
指针移动的单位都是以bytes/字节为单位
只有一种情况特殊:
t模式下的read(n),n代表的字符个数

with open('aaa.txt',mode='rt',encoding='utf-8') as f:
	res = f.read(4)
	print(res)

f.seek(n,模式):n指的是移动的字节个数
模式有三种:
0:参照物是文件开头位置

f.seek(9,0)
f.seek(3,0) #3

1:参照物是当前指针所在位置

f.seek(9,1)
f.seek(3,1) #12

#2:参照物是文件末尾位置,应该倒着移动

f.seek(-9,2) #3
f.seek(-3,2) #9

#强调:只有0模式可以在t模式下使用,1、2必须在b弄下用
f.tell() #获取文件指针当前位置

#示范

with open ('aaa.txt',mode='rb') as f:
	f.seek(9,0)
	f.seek(3,0)
	print(f.tell())
	#f.read(4,0)
	#res=f.read() #字符不足,不显示结果
	res = f.read()
	print(res.decode('utf-8'))

with open('aaa.txt',mode='rb') as f:
	f.seek(9,1)
	f.seek(3,1)
	print(f.tell())

with open ('aaa.txt',mode='rb') as f:
	f.seek(-9,2)
	f.seek(-3,2)
	print(f.tell())
	print(f.read().decode('utf-8'))

猜你喜欢

转载自blog.csdn.net/weixin_47237915/article/details/114652215
今日推荐