文件章的疑问
filename = "learn_python.txt"
while True:
print("Please input your name,If you want to quit, input 'q'\n")
guest_name = input("What is your name?>")
if guest_name == "q":
break
else:
print("Welcome!"+ guest_name)
with open(filename,"w") as f:
f.write(guest_name + "\n")
with open(filename,encoding='UTF-8') as file_object:
lines = file_object.readlines()
for line in lines:
line = line.strip()
print(f)
结果是
<_io.TextIOWrapper name='learn_python.txt' mode='w' encoding='UTF-8'>
本文介绍如何使用Python实现文件的读写,包括while循环获取用户输入,判断是否退出,并将输入写入'learn_python.txt'文件。最后,展示了如何以UTF-8编码读取并逐行打印文件内容。

被折叠的 条评论
为什么被折叠?



