我有一个打印信息的功能。然后我想将这些消息保存到文件中。但是当我使用writing_in_log = True
def print_and_log(message):
if write_in_log is True:
logFile = open("log", "w+")
logFile.write(message)
logFile.close()
时,它只在我的文件中写下最后一条消息
var array = [{ fullName: "Tony Stark", classRoom: "XI", city: "NYC", job: "Engineer", studies: "MIT", markAV: 10 }, { fullName: "Hulk Green", classRoom: "IV", city: "Beijing", job: "Physicist", studies: "Harvard", markAV: 9 }, { fullName: "Thor Norse", classRoom: "XX", city: "Oslo", job: "God", studies: "U of Norway", markAV: 8.5 }, { fullName: "Bruce Wayne", classRoom: "XIX", city: "Gotham", job: "Richamn", studies: "U of Gotham", markAV: 8 }, { fullName: "Peter Parker", classRoom: "XI", city: "NYC", job: "Photographer", studies: "Syracuse U", markAV: 7.5 }],
hash = Object.create(null),
result = [];
array.forEach(function (o) {
if (!hash[o.city]) {
hash[o.city] = [];
result.push(hash[o.city]);
}
hash[o.city].push(o);
});
console.log(result);
答案 0 :(得分:0)
我认为您在打开文件时没有使用'a'
参数:
with open('file.txt', 'a') as file:
file.write('function parameter')
答案 1 :(得分:0)
您可能会使用open(yourfile, 'w')
为每次写入打开文件,这会在您写入之前删除文件中的所有内容。如果您要附加到自己的文件,请使用open(yourfile, 'a')
。
如果这不是错误,我们需要有关您正在做的事情的更多信息,即代码的相关部分。