开发者社区 问答 正文

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-06-02 20:36:31 728 分享 版权
1 条回答
写回答
取消 提交回答
  • 代码没什么问题,我觉得可能是你打开txt忘关了,没刷新吧?你也可以给代码加上flush,强行写入看看

    2020-06-02 20:36:37
    赞同 展开评论
问答地址: