python操作文件模式实例分析
更新:HHH   时间:2023-1-7


这篇“python操作文件模式实例分析”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“python操作文件模式实例分析”文章吧。

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操作文件模式实例分析”这篇文章的内容,相信大家都有了一定的了解,希望小编分享的内容对大家有帮助,若想了解更多相关的知识内容,请关注天达云行业资讯频道。

返回大数据教程...