【发布时间】:2018-06-30 18:54:54
【问题描述】:
我已将字符串存储在一个变量中。我想将它附加到一个文本文件中。我怎样才能做到这一点?
def readyaml(abs_read_path,):
with open(abs_read_path, 'r') as stream, open("instanceinput.txt",'w') as fnew:
try:
content = yaml.load(stream)
instance = content['instance']
dump = instance[0]
print dump
fnew.write(dump)
except yaml.YAMLError as exc:
print(exc)
stream.close()
fnew.close()
readyaml(abs_read_path)
【问题讨论】:
-
您可能想阅读file operation 和PEP 434 上的文档。您会注意到,在使用上下文管理器时,您不必
close文件对象。实际错误显然是由于dump不是字符串。你检查dump的类型了吗?
标签: python