COURSE 2 – USING PYTHON TO INTERACT WITH THE OPERATING SYSTEM

Module 7: Final Project

GOOGLE IT AUTOMATION WITH PYTHON PROFESSIONAL CERTIFICATE

Complete Coursera Study Guide

Last updated:

INTRODUCTION – Final Project

In this module, you’ll put everything you’ve learned so far into action! You’ll apply your scripting knowledge to tackle a challenging final project: writing a script that scans for a specific error in the log files. You’ll create a problem statement to understand the challenge, conduct some research to see what options are available, then begin planning how you intend to solve the problem. Lastly, you’ll write the code to implement your solution!

Learning Objectives

  • Approach a problem using frameworks and best practices
  • Formulate a problem statement to understand the inputs and outputs of a script
  • Create a planned approach to solve the problem
  • Write a complex script to solve a problem

LOG ANALYSIS USING REGULAR EXPRESSIONS

1. What will the following command return?

grep "ERROR Tried to add information to closed ticket" syslog.log
  • A duplicate file of syslog with the “Tried to add information to closed ticket” errors removed
  • All the closed tickets in syslog 
  • All the ERROR logs in which the system tried to add information to closed ticket (CORRECT)
  • All the ERROR logs in syslog

Correct

2. You can reverse the order of the sort using the reverse parameter. What type of argument does the reverse parameter take? 

  • Operator 
  • Sort
  • Boolean (CORRECT)
  • Values

Correct

3. What is the primary advantage of using regular expressions when writing automation scripts to process a system log and generate reports from log files? 

  • Flexible pattern matching for extracting specific data from log entries (CORRECT)
  • Simplify the process of creating log files
  • Automate the installation of log analysis software
  • Enhance the visual presentation of log data in reports

Correct

4. While you were working with the log file named syslog.log, what command did you use to view the file? 

  • cat file syslog.log
  • grep syslog.log
  • cat syslog.log (CORRECT)
  • search syslog.log

Correct

5. What would you expect the command grep “ERROR Ticket doesn’t exist” syslog.log to return?

  • All ERROR logs in syslog.log with the error message “Ticket doesn’t exist”  (CORRECT)
  • All ERROR logs in syslog.log
  • All logs in syslog.log except the ones with the error message “Ticket doesn’t exist” 
  • All logs in syslog.log that do not have an existing ticket

Correct

6. What is the Python module used to perform similar tasks to the Unix command grep for filtering log data? 

  • logfilter module
  • re (Regular Expression) module (CORRECT)
  • logsearch module
  • grep module

Correct

7. Evaluate the following problem statement: “I want to create a script to sort files.” What’s missing? 

  • The problem statement does not specify what the script is supposed to do.
  • The problem statement does not specify the programming language.
  • The problem statement is complete.
  • The problem statement does not specify what files to sort. (CORRECT)

Correct

8. Which of the following commands would convert a csv file named error_message.csv into HTML file named errors.html? 

  • /csv_to_html.py error_message.csv /var/www/html/<errors.html>.html
  • /csv_convert_html.py error_message.csv /var/www/html/<errors>.html
  • ./csv_to_html.py error_message.csv /var/www/html/<errors>.html (CORRECT)
  • /html_to_csv.py error_message.csv /var/www/html/<errors>.html

Correct

9. Once you’ve understood the problem statement, what should be the second step for your coding project?

  • Planning
  • Writing the code
  • Researching available tools (CORRECT)
  • Writing a design document

Right on! You’ll want to figure out how to tackle the problem with tools such as the Python Standard Library.

10. Which task can you accomplish by using regular expressions in log analysis? 

  • Parsing log entries to extract specific fields (CORRECT)
  • Sorting log entries based on timestamps
  • Counting the total number of log entries in a file
  • Converting log data into graphical charts

Correct

11. Complete the sentence for the following Python regular expression: To match a string stored in a line variable, we use the search() method by defining a_____.

  • span
  • line
  • pattern (CORRECT)
  • log

Correct

12. When sorting this dictionary:

fruit = {"oranges": 3, "apples": 5, "bananas": 7, "peaches": 2}
What will the following line of code return?
	sorted(fruit.items(), key=operator.itemgetter(1))
  •  [('apples', 5), ('bananas', 7), ('oranges', 3), (peaches, 2)]
  • sorted fruit = {"oranges": 3, "apples": 5, "bananas": 7, "peaches": 2}
  • [(peaches, 2), ('oranges', 3), ('apples', 5), ('bananas', 7)] (CORRECT)
  • [('bananas', 7), ('apples', 5), ('oranges', 3), (peaches, 2)]

Correct

13. Which of the following is a potential pitfall of automation in Python? 

  • It increases the amount of manual work required
  • It makes the system more prone to errors
  • It improves the overall efficiency of the system
  • It limits the system’s ability to adapt to changes (CORRECT)

Correct

14. Why are regular expressions useful? 

  • They allow us to search and manipulate text based on patterns (CORRECT)
  • They allow us to create graphical user interfaces
  • They allow us to perform mathematical operations
  • They allow us to connect to databases

Correct

15. What is the method used to match a string stored in a line variable by defining a pattern? 

  • re.search() (CORRECT)
  • re.match()
  • re.find()
  • re.string()

Correct

16. What is the primary purpose of using regular expressions in log analysis? 

  • To encrypt log data for enhanced security
  • To compress log files and save storage space
  • To extract specific patterns and information from unstructured log data (CORRECT)
  • To format log messages for easier readability

Correct

17. Which of the following is true about using regular expressions? 

  • They reduce the need of error checking in code
  • They don’t allow for flexible pattern matching in strings
  • They can simplify complex string processing tasks (CORRECT)
  • They can be used only in Python

Correct

18. Why do you need to know how to write automation scripts that process a system log and generate reports from the log files? 

  • To increase job complexity without adding tangible value to the organization
  • To eliminate the need for manual log analysis, saving time, and improving efficiency (CORRECT)
  • To showcase proficiency in scripting languages without practical applications
  • To impress colleagues with technical skills and programming expertise

Correct

19. What would you expect the command grep “ERROR” syslog.log to return?

  • All ERROR logs in syslog.log (CORRECT)
  • The ERROR messages in syslog.log 
  • All logs in syslog.log
  • All ERROR logs in syslog.log that have no corresponding error message

Correct

20. Which argument can be used with the sorted() function to sort a dictionary’s items based on their values in descending order? 

  • sorted(dictionary, reverse=True)
  • sorted(dictionary, key=lambda x: x[1], reverse=True)
  • sorted(dictionary.values(), reverse=True)
  • sorted(dictionary.items(), key=lambda x: x[1], reverse=True) (CORRECT)

Correct

21. If there is no csv file named user_emails.csv, what will the command nano user_emails.csv return? 

  • A new csv file named user_emails.csv (CORRECT)
  • A new csv file named nano user_emails.csv 
  • An error message
  • A new csv file named user_emails.csv populated withusers emails

Correct

22. What will the following command return?

grep "ERROR" syslog.log
  • A duplicate file of syslog with the errors removed
  • All the ERROR logs in syslog (CORRECT)
  • All the INFO logs in syslog
  • All the usernames in syslog

Correct

23. When sorting this dictionary:

fruit = {"oranges": 3, "apples": 5, "bananas": 4, "pears": 2}

What will the following line of code return?

sorted(fruit.items(), key=operator.itemgetter(1))
  •  [('bananas', 4), ('apples', 5), ('oranges', 3), ('pears', 2)]
  • sorted fruit = {"oranges": 3, "apples": 5, "bananas": 4, "pears": 2}
  • [('apples', 5), ('bananas', 4), ('oranges', 3), ('pears', 2)]
  • [('pears', 2), ('oranges', 3), ('bananas', 4), ('apples', 5)] (CORRECT)

Correct

24. When sorting this dictionary:

fruit = {"oranges": 3, "apples": 5, "bananas": 7, "pears": 2}

What will the following line of code return?

sorted(fruit.items(), key=operator.itemgetter(1))
  • [('bananas', 7), ('apples', 5), ('oranges', 3), ('pears', 2)]
  • [('pears', 2), ('oranges', 3), ('apples', 5), ('bananas', 7)] (CORRECT)
  • [('apples', 5), ('bananas', 7), ('oranges', 3), ('pears', 2)]
  • sorted fruit = {"oranges": 3, "apples": 5, "bananas": 7, "pears": 2}

Correct

25. What syntax would you use to enlist all the ERROR messages of a specific kind?

  • grep ERROR [file-name] [message]
  • grep [file-name] [message] ERROR
  • grep ERROR [message] [file-name] (CORRECT)
  • grep [file-name] ERROR [message]

Correct

26. In Python, regular expressions are typically handled using which module? 

  • sys
  • math
  • re (CORRECT)
  • os

Correct

27. How does the sorted() function sort items in a Python dictionary? 

  • Sorts dictionary keys in descending order and returns a list of keys
  • Sorts dictionary keys in ascending order and returns a list of keys (CORRECT)
  • Sorts dictionary items based on their keys in ascending order and returns a list of items
  • Sorts dictionary items based on their values in ascending order and returns a list of items

Correct

28. If there is no python script named ticky_check.py, what will the command nano ticky_check.py return?

  • A new python script named nano ticky_check.py
  • An error message
  • A new csv file named ticky_check.py
  • A new python script named ticky_check.py (CORRECT)

Correct

CONCLUSION – Final Project

In summary, this section empowers you to put your acquired knowledge into practice through a significant final project. Leveraging your scripting skills, you’ll tackle the challenge of developing a script capable of scanning log files for specific errors.

The journey begins with formulating a clear problem statement to understand the task, followed by thorough research to explore available options. Strategic planning guides your approach to solving the problem, culminating in the practical implementation of your solution through coding.