开发者社区> 问答> 正文

Python通过print写入内容到文件报错

log=open("login.txt","w")
print>>log,"a"
print>>log,"b"

运行报错

Traceback (most recent call last):
  File "/Users/jiangyd/PycharmProjects/untitled16/b.py", line 47, in <module>
    print>>log,"a"
TypeError: unsupported operand type(s) for >>: 'builtin_function_or_method' and '_io.TextIOWrapper'. Did you mean "print(<message>, file=<output_stream>)"?

展开
收起
水果黄瓜 2021-10-24 11:19:55 656 0
2 条回答
写回答
取消 提交回答
  • 可以通过以下方式

    f=open("a.log","a+")
    print("tet",file=f)
    
    2021-10-25 21:25:24
    赞同 展开评论 打赏
  • 网络规划设计师、敏捷专家、CISP、ITSS服务经理、ACA全科目、ACP4项、ACE、CBP、CDSP、CZTP等。拥有 PRINCE2 Foundation/Practitioner、CCSK、ITIL、ISO27001、PMP等多项国际认证。 专利5+、期刊10+、知识产权师。核心期刊审稿人。

    您好,不支持这种写法了。例如

    Instead of writing print >> sys.stderr, expression, it's instead necessary to write print(expression, file=sys.stderr).

    2021-10-24 11:46:47
    赞同 1 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
From Python Scikit-Learn to Sc 立即下载
Data Pre-Processing in Python: 立即下载
双剑合璧-Python和大数据计算平台的结合 立即下载