如何解决覆盖新生成文件的内容
该代码每30秒生成一个带有日期/时间的文件,然后在self.log中键入“ key”。问题是当生成新文件并且键入“ key”时,它只是将自身附加到底部,而不会覆盖新生成的文件中的旧内容。 感谢您的帮助:)
def report(self):
if self.log:
print(self.log)
starttime = time.time()
while True:
timestr = time.strftime("%Y%m%d-%H%M%s")
fileloc = f'C:/logging/pylogger/{timestr}.txt'
with open(fileloc,mode='w') as f:
f.seek(0)
f.truncate()
for key in self.log:
f.write(key)
time.sleep(30.0 - ((time.time() - starttime) % 30.0))
解决方法
您的问题对我来说还不是很清楚,但是如果我正确理解,则一旦编写,就需要从列表中删除这些元素:
import React from 'react';
import CKEditor from '@ckeditor/ckeditor5-react';
import ClassicEditor from '@ckeditor/ckeditor5-build-classic';
function App() {
return (
<div className="App">
<CKEditor
editor={ClassicEditor}
data="<p>Hello from CKEditor 5!</p>"
onInit={editor => {
// You can store the "editor" and use when it is needed.
console.log('Editor is ready to use!',editor);
}}
onChange={(event,editor) => {
const data = editor.getData();
console.log({ event,editor,data });
}}
config={{
toolbarLocation: 'bottom'
}}
/>
</div>
);
}
export default App;
只需在您的for循环中添加def report(self):
if self.log:
print(self.log)
starttime = time.time()
while True:
timestr = time.strftime("%Y%m%d-%H%M%S")
fileloc = f'C:/logging/pylogger/{timestr}.txt'
with open(fileloc,mode='w') as f:
f.seek(0)
f.truncate()
for key in self.log:
f.write(key)
self.log.remove(key)
time.sleep(30.0 - ((time.time() - starttime) % 30.0))
,那么一旦写入,这些值就会从列表中删除,并且当您在30秒后到达下一个周期时,新文件将仅包含新值。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。