当前位置: 首页 > 图灵资讯 > 行业资讯> python操作文件模式的介绍

python操作文件模式的介绍

发布时间:2024-06-28 21:39:06

1、只读模式(只读数据r)。

f=open('my_file.txt','rb')
f_bytes=f.read()
print(f_bytes)
print(f_bytes.decode('utf-8'))
f.close()

2、只写模式(重写数据w),以二进制的方式写入数据或只写模式添加数据。

write_file=open('demo1.jpg',mode='wb')
read_file=open('demo.jpg',mode='rb')
write_file.write(read_file.read())
read_file.close()
write_file.close()

3、读写模式(附加数据a),如果不想覆盖原始数据,可以使用附加写入数据。

read_write_file=open('append_file.txt',mode='r+',encoding='utf-8')
print(read_write_file.read())
read_write_file.write('\n努力向上,每天向上')
print(read_write_file.read())
read_write_file.close()

以上就是python操作文件模式的介绍,希望对大家有所帮助。更多Python学习指导:python基础教程

本文教程操作环境:windows7系统Python 3.9.1,DELL G3电脑。

相关文章

如何让vim支持python3

如何让vim支持python3

2025-09-12
python2.7和3.6区别有哪些

python2.7和3.6区别有哪些

2025-09-12
python3有serial库吗

python3有serial库吗

2025-09-12
python中w、r表示什么意思

python中w、r表示什么意思

2025-09-12
python中如何把list变成字符串

python中如何把list变成字符串

2025-09-12
python命名空间是什么

python命名空间是什么

2025-09-12