![](/img/trans.png)
[英]How to write an output of a command to stdout and a file in Python3?
[英]how to write a function output in a file in python3
假设我有一个 class 的功能
class A:
def func1():
print(output.of.api)
def func2():
print(output.of.another.api)
#i make the required objects
func1_object = A()
func1_object.func1()
我需要将这些对象的 output 写入 file.txt
所以我这样做
f = open("file.txt", "w")
f.write(func1_object.func())
这显然给了我一个错误
TypeError: write() argument must be str, not None
我尝试了基本的谷歌解决方案,显然对我不起作用
提前感谢您帮助我!
func()
不返回任何内容。 您必须退回您的 output。
class A:
def func1():
return output.of.api
def func2():
return output.of.another.api
class A:
def func1():
with open('file.txt','w') as f:
print(str(output.of.api),file=f)
或按照@rbcvi 的建议返回值并写入文件
class A:
def func1():
return output.of.api
with open('file.txt', 'w') as f:
f.write((func1_object.func())
目前您的 function 不返回任何内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.