开发者社区 问答 正文

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



展开
收起
kun坤 2020-06-14 07:00:52 796 分享 版权
2 条回答
写回答
取消 提交回答
  • 不懂的太多

    我试的一次就可以

    2022-03-23 21:06:23
    赞同 展开评论
  • 代码没什么问题,我觉得可能是你打开txt忘关了,没刷新吧?你也可以给代码加上flush,强行写入看看

    2020-06-14 07:00:58
    赞同 展开评论
问答地址: