开发者社区 问答 正文

print写入文件的问题-print报错

"

Ubuntu+python3+IDLE,为什么通过print写入文件的时候,第一次总是无效,下面的代码执行第二次之后文件中才写入对应print的内容?

-------------------------------

try:

        newdata=open('talk.txt','w')
        print("you are the best",file=newdata)
except IOError:
        print('The file is missing')
newdata.close



"

展开
收起
montos 2020-05-31 15:00:09 835 分享 版权
2 条回答
写回答
取消 提交回答
  • 不懂的太多

    \n试下 print("you are the best\n",file=newdata)

    2022-03-23 20:59:55
    赞同 展开评论
  • "

    代码没什么问题,我觉得可能是你打开txt忘关了,没刷新吧?你也可以给代码加上flush,强行写入看看

    "
    2020-05-31 15:00:17
    赞同 展开评论
问答地址: