im using the following code to read file , and write some lines which contain certain word
with open('access.log') as f:
for line in f:
logdate = datetime.strptime(line.split(',')[0], '%Y-%m-%d %H:%M:%S')
if logdate >= datetime.now() - timedelta(minutes=10):
if 'Busy' in line:
file = open ('newfile.txt' , 'w')
file.write(line)
file.close()
I still cant get the file created and data inserted , what do i miss here ?
You keep overwriting using w
use a
to append or open outside the loop.
with open('access.log') as f, open ('newfile.txt' , 'w') as file:
for line in f:
logdate = datetime.strptime(line.split(',')[0], '%Y-%m-%d %H:%M:%S')
if logdate >= datetime.now() - timedelta(minutes=10) and 'Busy' in line:
file.write(line)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.