分别采用open-close、with-open两种方式,将自己的学号、姓名、班级等信息分行写入文本文件a.txt中;
分别采用open-close、with-open两种方式,读取文本文件a.txt中的全部内容并输出
分别采用open-close、with-open两种方式,读取文本文件a.txt中的前3个字符并输出;
f = open("a.txt", "w")
f.write("学号:001,姓名:小明,班级:数学201,性别:男")
f.close()
with open("a.txt","w") as f:
f.write("学号:001,姓名:小明,班级:数学201,性别:男")
f.close()
f = open("a.txt", "r")
print(f.read())
f.close()
with open("a.txt", "r") as f:
print(f.read())
f.close()
f = open("a.txt", "r")
print(f.read(3))
f.close()
with open("a.txt", "r") as f:
print(f.read(3))
f.close()