POC Automation & Scripting 2 — Questions and Answers
Question 1: Which Python module provides the `run()` function for executing shell commands and capturing output?
- os
- subprocess (Correct answer)
- shutil
- commands
Correct answer: subprocess
`subprocess.run()` executes a command, waits for it to complete, and returns a CompletedProcess instance with stdout/stderr.
Question 2: In a Python automation script, what does `subprocess.run(['ls', '-la'], capture_output=True, text=True)` return?
- A string of the command output
- A CompletedProcess object with stdout and stderr as strings (Correct answer)
- A file object
- An integer exit code only
Correct answer: A CompletedProcess object with stdout and stderr as strings
`capture_output=True` and `text=True` make `run()` return a CompletedProcess whose `.stdout` and `.stderr` are decoded strings.
Question 3: Which `schedule` library method runs all pending scheduled jobs?
- schedule.run_jobs()
- schedule.run_pending() (Correct answer)
- schedule.execute()
- schedule.tick()
Correct answer: schedule.run_pending()
`schedule.run_pending()` checks all registered jobs and runs any whose next run time has passed.
Question 4: What is the purpose of the `watchdog` library in Python automation?
- Monitor filesystem events and trigger callbacks (Correct answer)
- Watch CPU usage and kill runaway processes
- Schedule jobs at fixed intervals
- Restart crashed Python processes automatically
Correct answer: Monitor filesystem events and trigger callbacks
`watchdog` monitors file system changes (create, modify, delete) and dispatches events to handler classes.
Question 5: Which context manager ensures a file is properly closed even if an exception occurs during reading?
- try/finally without `with`
- `with open(filename) as f:` (Correct answer)
- `open(filename, safe=True)`
- `file.auto_close()`
Correct answer: `with open(filename) as f:`
The `with` statement calls `__exit__` on the file object, closing it automatically even if an exception is raised inside the block.
Question 6: In Python's `argparse`, what does `add_argument('--verbose', action='store_true')` do?
- Stores the string 'True' when --verbose is passed
- Sets the attribute to True when the flag is present, False otherwise (Correct answer)
- Requires a value to follow --verbose
- Raises an error if --verbose is not provided
Correct answer: Sets the attribute to True when the flag is present, False otherwise
`action='store_true'` sets the destination attribute to `True` when the flag appears and `False` when it is absent.
Question 7: Which Python function sends an HTTP GET request when using the `requests` library?
- requests.fetch()
- requests.get() (Correct answer)
- requests.http_get()
- requests.open()
Correct answer: requests.get()
`requests.get(url)` sends an HTTP GET request and returns a `Response` object with `.status_code`, `.text`, and `.json()` method.
Which Python module provides the `run()` function for executing shell commands and capturing output?