filename='10-3guest.txt'
your_name=input("请您输入您的用户名")
#open()前后参数不能写错
#读写模式
with open(filename,'w') as file_object:
file_object.write(your_name)
contents=file_object.read()
print(contents)
错误原因,文件打开方式选择只写。
Python3 File read() 方法 | 菜鸟教程 (runoob.com)
相关参数
修正后代码
filename='10-3guest.txt'
your_name=input("请您输入您的用户名")
#open()前后参数不能写错
#读写模式
with open(filename,'r+') as file_object:
file_object.write(your_name)
contents=file_object.read()
print(contents)