编程新手,尝试学习一些基础知识。尝试让它在Python 3.x中工作
我想增强writeline函数以始终添加,\n这样我就不必在每个字符串之后都使用它。我写了上百行。
在我的代码下面,我做错了什么?
def writextra(self, string):
self.writelines(string + '\n')
return None
text_file=open('some_file.txt', 'w')
text_file.writextra('hello world')
text_file.writextra('line two')
Error is:
AttributeError: '_io.TextIOWrapper' object has no attribute 'writextra'发布于 2018-07-18 00:31:15
为了快速修复,您不应该在函数中添加self参数,而应该添加f_out参数,然后调用函数writeextra(text_file, 'hello world')
def writextra(f_out, string):
return f_out.writelines(string + '\n')
text_file=open('some_file.txt', 'w')
writextra(text_file, 'hello world')
writextra(text_file, 'line two')https://stackoverflow.com/questions/51386214
复制相似问题