""" Author:Zhou Create_Date:2022年06月18日--10:08 Tool:PyCharm Software: PyCharm """ """open('pi_digits')会返回一个表示文件pi_digits的对象,将其对象赋给file_object""" # with open('pi_digits') as file_object: # ""
Author:Zhou
Create_Date:2022年06月18日--10:08
Tool:PyCharm
Software: PyCharm
"""
"""open('pi_digits')会返回一个表示文件pi_digits的对象,将其对象赋给file_object"""
# with open('pi_digits') as file_object:
# """读取文件内容"""
# contents = file_object.read()
# print(contents.rstrip())
"""逐行读取文本内容"""
# with open('pi_digits') as file_object:
# for line in file_object:
# print(line.rstrip())
"""创建一个包含文件各行内容的列表"""
# filename = "pi_digits"
# with open(filename) as file_object:
# lines = file_object.readlines()
# for line in lines:
# print(line.rstrip())
"""
写入文件
'w':写,如果指定的文件存在内容,则会清楚该文件内容
'a':附加 不覆盖
'r':读取文件
'r+':读写模式
"""
filename = 'pi_digits'
with open(filename, 'w') as file_object:
file_object.write('1997')
file_object.close()
with open(filename) as file_object:
context = file_object.read()
print(context)
# 此时之前的文件内容是被写进去的1997覆盖了