Using Python to Interact with the Operating System Week 7 Solution

Log Analysis Using Reguar Expression

Using Python to Interact with the Operating System WEEK 7 Qwiklabs Assessment Coursera  by Google. Welcome to Using Python to Interact with the Operating System. You’re joining thousands of learners currently enrolled in the course. This course is a part of Google IT Automation with Python Professional Certificate.

I’m excited to have you on my channel and look forward to your contributions to the learning community. By the end of this course:

  1. you’ll be able to manipulate files and processes on your computer’s operating system. You’ll also have learned about regular expressions a very powerful tool for processing text files
  2. you’ll get practice using the Linux command line on a virtual machine.

And, this might feel like a stretch right now, but you’ll also write a program that processes a bunch of errors in an actual log file and then generates a summary file. That’s a super useful skill for IT Specialists to know.

-:Skills you will learn:-

  • Setting up your Development Environment
  • Regular Expression (REGEX) 
  • Testing in Python 
  • Automating System Administration Tasks with Python
  • Bash Scripting

~Course Link:

Qwiklabs Assessment : “Log Analysis Using Reguar Expression”


					#!/usr/bin/env python3
import re
import csv
import operator
import sys

per_user = {}
errors = {}

logfile = 'syslog.log'
f = open(logfile, 'r')

errorfile = 'error_message.csv'
userfile = 'user_statistics.csv'

for log in f:
        result ="ticky: ([\w+]*):? ([\w' ]*) [\[[0-9#]*\]?]? ?\((.*)\)$", log)
        if not in errors.keys():
                errors[] = 0
        errors[] += 1
        if not in per_user.keys():
                per_user[] = {}
                per_user[]["INFO"] = 0
                per_user[]["ERROR"] = 0

        if == "INFO":
                per_user[]["INFO"] += 1
        elif == "ERROR":
                per_user[]["ERROR"] += 1

errors = sorted(errors.items(), key = operator.itemgetter(1), reverse = True)
per_user = sorted(per_user.items())

errors.insert(0, ('Error', 'Count'))

f = open(errorfile, 'w')
for error in errors:
        a,b = error

f = open(userfile, 'w')
for stats in per_user:
        a, b = stats


Leave a Reply

Your email address will not be published. Required fields are marked *