WEEK 4 PYTHON AUTOMATION

Ace your homework & exams now with Quizwiz!

Which directory is NOT listed in the PATH variable by default?

/usr/sbin/temp (NOT by default) /bin (BY DEFAULT) /sbin (BY DEFAULT) /usr/local/sbin (BY DEFAULT)

What type of object does a run function return?

CompletedProcess (This object includes information related to the execution of a command.)

What does the copy method of os.environ do?

Creates a new dictionary of environment variables (The copy method of os.environ makes a new copy of the dictionary containing the environment variables, making modification easier.)

Which I/O stream are we using when we use the input function to accept user input in a Python script?

STDIN is the standard I/O stream for input.

When a child process is run using the subprocess module, which of the following are true? (check all that apply)

The child process is run in a secondary environment. (To run the external command, a secondary environment is created for the child subprocess, where the command is executed.) The parent process is blocked while the child process finishes (While the parent process is waiting on the subprocess to finish, it's blocked, meaning the parent can't do any work until the child finishes.) Control is returned to the parent process when the child process ends (After the external command completes its work, the child process exits, and the flow of control returns to the parent.)

What is the meaning of an exit code of 0?

The program ended successfully An exit value of 0 always indicates the program exited without error.

Which of the following is a Unicode standard used to convert an array of bytes into a string?

UTF -8 This encoding is part of the Unicode standard that can transform an array of bytes into a string.

How can you change the current working directory where a command will be executed?

Use the cwd parameter (This will change the current working directory where the command will be executed.)

hich of the following are true about parsing log files? (Select all that apply.)

You should parse log files line by line (Since log files can get pretty large, it's a good idea to parse them one line at a time instead of loading the entire file into memory at once.) It is efficient to ignore lines that don't contain the information we need (We can save a lot of time by not parsing lines that don't contain what we need.) We have to open() the log file first (Before we can parse our log file, we have to use the open() or with open() command on the file first.)

When using the run command of the subprocess module, what parameter, when set to True, allows us to store the output of a system command?

capture_output (The capture_output parameter allows us to get and store the output of the system command we're using.)

Which keyword will return control back to the top of a loop when iterating through logs?

continue (The continue statement is used to return control back to the top of a loop.)

Which method do you use to prepare a new environment to modify environment variables?

copy (Calling this method of the os.environ dictionary will copy the current environment variables to store and prepare a new environment)

Which of the following is a data structure that can be used to count how many times a specific error appears in a log?

dictionary (A dictionary is useful to count appearances of strings.)

Which command will print out the exit value of a script that just ran successfully?

echo $? Echo will print out the exit value (question mark variable) of a script that just ran successfully.

Which command will create a new environment variable?

export This command will create a new environment variable, and give it a value.

We're using the same syslog, and we want to display the date, time, and process id that's inside the square brackets. We can read each line of the syslog and pass the contents to the show_time_of_pid function. Fill in the gaps to extract the date, time, and process id from the passed line, and return this format: Jul 6 14:01:23 pid:29440.

import re import sys def show_time_of_pid(line): pattern = r"([a-zA-Z]+ \d+ \d+:\d+:\d+).*\[(\d+)\]\:" ##(parenthesis) creates groups, here there will be two groups with subcript (1) and (2)## result = re.search(pattern, line) return "{} pid:{}".format(result.group(1), result.group(2))

Which statements are true about input and raw_input in Python 2? (select all that apply)

input performs basic math operations (In Python 2, input evaluates the user's input as an expression.) raw_input gets a string from the user (raw_input gets a raw string from the user.)

When searching log files using regex, which regex statement will search for the alphanumeric word "IP" followed by one or more digits wrapped in parentheses using a capturing group?

r"IP \((\d+)\)$" (This expression will search for the word "IP" followed by a space and parentheses. It uses a capture group and \d+ to capture any digit characters found in the parentheses.)

A system command that sends ICMP packets can be executed within a script by using which of the following?

subprocess.run (This function will execute a system command such as ping)

Where are the command line arguments stored?

sys (The list of arguments are stored in the sys module.)

You have created a Python script to read a log of users running CRON jobs. The script needs to accept a command line argument for the path to the log file. Which line of code accomplishes this?

syslog=sys.argv[1] This will (A dictionary is useful to count appearances of strings.assign the script's first command line argument to the variable "syslog".

Which line of code from the seconds.py script will convert all integer inputs into seconds?

to_seconds(hours, minutes, seconds) This line of code uses a function to convert the number of hours, minutes, and seconds into seconds

Which of the following is a correct printout of a dictionary?

{'carrots':100, 'potatoes':50, 'cucumbers': 65} {50:'apples', 55:'peaches', 15:'banana'} A dictionary stores key:value pairs.


Related study sets

Financial Market and Institutions extra questions

View Set

Test 2: Chapter 5-8 (Cross cultural psychology)

View Set

Astronomy Ch. 6 Practice Questions

View Set

Pharmacology 9.2 Pretest/Posttest

View Set