Automation & Scripting Flashcards
7 cards from real POC practice questions. Tap to flip, then mark Knew It or Still Learning โ missed cards come back until you master them.
Read the first 7 Automation & Scripting flashcards as text
Which Python module provides the `run()` function for executing shell commands and capturing output?
Answer: subprocess
`subprocess.run()` executes a command, waits for it to complete, and returns a CompletedProcess instance with stdout/stderr.
In a Python automation script, what does `subprocess.run(['ls', '-la'], capture_output=True, text=True)` return?
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.
Which `schedule` library method runs all pending scheduled jobs?
Answer: schedule.run_pending()
`schedule.run_pending()` checks all registered jobs and runs any whose next run time has passed.
What is the purpose of the `watchdog` library in Python automation?
Answer: Monitor filesystem events and trigger callbacks
`watchdog` monitors file system changes (create, modify, delete) and dispatches events to handler classes.
Which context manager ensures a file is properly closed even if an exception occurs during reading?
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.
In Python's `argparse`, what does `add_argument('--verbose', action='store_true')` do?
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.
Which Python function sends an HTTP GET request when using the `requests` library?
Answer: requests.get()
`requests.get(url)` sends an HTTP GET request and returns a `Response` object with `.status_code`, `.text`, and `.json()` method.