将输出重定向到文件中:
将print语句输出重定向到当前路径下的 log.txt 文件中:
with open("log.txt","at") as f :
print("hello world!", file=f)
配合print的sep和end参数,可以改变输出内容的分隔符或结束符:
with open("log.txt","at") as f :
print("张三", 12345678, sep=":", end=";\n", file=f)
查看文件内容: