首页IT科技python操作文本文件(python操作文件模式的介绍)

python操作文本文件(python操作文件模式的介绍)

时间2025-08-04 19:18:40分类IT科技浏览4718
导读:1、只读模式(只读数据r 。 f=...

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基础教程

创心域SEO版权声明:以上内容作者已申请原创保护,未经允许不得转载,侵权必究!授权事宜、对本内容有异议或投诉,敬请联系网站管理员,我们将尽快回复您,谢谢合作!

展开全文READ MORE
python3.8打开文件(python打开文件的两种方式)