Rak*_*esh 3 python attributes seek attributeerror
运行以下代码时出现“AttributeError: 'str' object has no attribute 'seek'”。有人可以指出问题出在哪里吗?
import re
import os
import time
regex = ' \[GC \((?<jvmGcCause>.*?)\).+?(?<jvmGcRecycletime>\d+\.\d+) secs\]'
read_line = True
def follow(thefile):
thefile.seek(0,os.SEEK_END)
while True:
lines = thefile.readline()
if not lines:
time.sleep(0.1)
continue
yield lines
if __name__ == '__main__':
logfile = r"/gc.log"
loglines = follow(logfile)
for line in loglines:
match = re.search(regex, line)
if match:
print('jvmGcCause: ' + +match.group(1))
print('jvmGcRecycletime: ' + match.group(2))
Run Code Online (Sandbox Code Playgroud)
在 python 中seek是一个文件对象的方法,你试图将它应用到一个字符串上。您必须先打开文件,然后调用seek打开的文件对象。
做这样的事情:
def follow(file_name):
with open filename as the_file:
the_file.seek(0, os.SEEK_END)
while True:
lines = the_file.readline()
if not lines:
time.sleep(0.1)
continue
yield lines
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4436 次 |
| 最近记录: |