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:
- 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
- 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:https://www.coursera.org/learn/python-operating-system
Qwiklabs Assessment : “Log Analysis Using Reguar Expression”
Codes:-
#!/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 = re.search(r"ticky: ([\w+]*):? ([\w' ]*) [\[[0-9#]*\]?]? ?\((.*)\)$", log)
if result.group(2) not in errors.keys():
errors[result.group(2)] = 0
errors[result.group(2)] += 1
if result.group(3) not in per_user.keys():
per_user[result.group(3)] = {}
per_user[result.group(3)]["INFO"] = 0
per_user[result.group(3)]["ERROR"] = 0
if result.group(1) == "INFO":
per_user[result.group(3)]["INFO"] += 1
elif result.group(1) == "ERROR":
per_user[result.group(3)]["ERROR"] += 1
errors = sorted(errors.items(), key = operator.itemgetter(1), reverse = True)
per_user = sorted(per_user.items())
f.close()
errors.insert(0, ('Error', 'Count'))
f = open(errorfile, 'w')
for error in errors:
a,b = error
f.write(str(a)+','+str(b)+'\n')
f.close()
f = open(userfile, 'w')
f.write("Username,INFO,ERROR\n")
for stats in per_user:
a, b = stats
f.write(str(a)+','+str(b["INFO"])+','+str(b["ERROR"])+'\n')