文件处理模式用于指定打开文件的方式,包括读取、写入、追加等操作。Python中常用的文件处理模式有以下几种:
with open('file.txt', 'r') as file:
data = file.read()
print(data)
with open('file.txt', 'w') as file:
file.write('Hello, World!')
with open('file.txt', 'a') as file:
file.write('Hello, World!')
with open('file.txt', 'r+') as file:
data = file.read()
file.write('Hello, World!')
with open('file.txt', 'rb') as file:
data = file.read()
print(data)
以上是一些常用的文件处理模式,你可以根据自己的需求选择适合的模式进行文件操作。在使用文件处理模式时,务必记得使用with语句来打开文件,以便在操作结束后自动关闭文件。
Copyright © Yisu Cloud Ltd. All Rights Reserved. 2018 版权所有
广州亿速云计算有限公司 粤ICP备17096448号-1
粤公网安备 44010402001142号 增值电信业务经营许可证编号:B1-20181529