开发者社区> 问答> 正文

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 791 0
2 条回答
写回答
取消 提交回答
  • 不懂的太多

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

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

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

    "
    2020-05-31 15:00:17
    赞同 展开评论 打赏
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载