我在互联网上到处搜索,但是我找不到如何使用python对日志文件进行gzip处理并保留日志文件的时间戳。我看了python提供的gzip函数,但是因为它读取数据然后输出,所以它覆盖了文件的时间戳。我需要它的行为就像对文件运行linuxgzip命令一样。有办法吗?在
try:
f_in=open(file,'rb')
f_out=gzip.open(file + '.gz','wb')
f_out.writelines(f_in)
f_out.close()
f_in.close()
# delete copy that gzip creates during gzip process
os.unlink(file)
except IOError, e:
print "Cant Gzip %s: File not found " % file
如果使用Python2.6,则zlib模块无法调整写入gz文件的mtime。你只剩下两种方法:
来自the documentation:
相关问题 更多 >
编程相关推荐