[英]How do you use open() with appending mode AND seek() in Python on Linux?
我有以下几点:
with open(file, 'a') as log:
#A bunch of code, some of it writes to log.
log.seek(0)
log.write(time.strftime(t_format))
seek()不适用于append,如果我使用“ w”,则文件的开头将被覆盖。 在文档中它说:“ ...'a'用于追加(在某些Unix系统上,这意味着所有写操作均会追加到文件末尾,而不管当前查找位置如何)”
有什么办法可以覆盖吗?
搜索的第二个参数允许相对于文件末尾进行搜索:
with open(filename, 'w') as log:
log.seek(0, 0)
log.write(time.strftime(t_format))
您还应该考虑使用Python的日志记录模块编写日志。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.