POC Automation & Scripting 3 — Questions and Answers
Question 1: Which Python module allows you to read and write `.env` files for environment variable management?
- envparse
- dotenv (python-dotenv) (Correct answer)
- configparser
- os.environ
Correct answer: dotenv (python-dotenv)
`python-dotenv` loads variables from a `.env` file into `os.environ` using `load_dotenv()`.
Question 2: What does `os.walk(top)` yield when traversing a directory tree?
- A list of all file paths as strings
- Tuples of (dirpath, dirnames, filenames) for each directory (Correct answer)
- Only file names without paths
- A generator of Path objects
Correct answer: Tuples of (dirpath, dirnames, filenames) for each directory
`os.walk()` yields `(dirpath, dirnames, filenames)` tuples, where `dirnames` are subdirectory names and `filenames` are file names in that directory.
Question 3: In Selenium WebDriver automation, which method waits up to a maximum time for a condition to be true before throwing a TimeoutException?
- driver.sleep()
- WebDriverWait(driver, timeout).until(condition) (Correct answer)
- driver.implicitly_wait()
- driver.pause(timeout)
Correct answer: WebDriverWait(driver, timeout).until(condition)
`WebDriverWait` polls the condition function every 500ms and raises `TimeoutException` if the condition isn't met within the timeout.
Question 4: Which `pathlib.Path` method creates a directory and all missing intermediate parents without error if the directory already exists?
- Path.mkdir()
- Path.mkdir(parents=True, exist_ok=True) (Correct answer)
- Path.makedirs()
- Path.create(recursive=True)
Correct answer: Path.mkdir(parents=True, exist_ok=True)
`parents=True` creates intermediate directories and `exist_ok=True` suppresses the `FileExistsError` if the target already exists.
Question 5: When automating CSV processing, which Python module provides `DictReader` to iterate rows as dictionaries?
- json
- csv (Correct answer)
- pandas
- tablib
Correct answer: csv
`csv.DictReader` reads each row using the first row's headers as keys, returning `OrderedDict` objects per row.
Question 6: What is the correct way to pass environment variables to a subprocess in Python?
- subprocess.run(cmd, env_vars={'KEY': 'val'})
- subprocess.run(cmd, env={'KEY': 'val'}) (Correct answer)
- subprocess.run(cmd, environment={'KEY': 'val'})
- os.setenv('KEY', 'val'); subprocess.run(cmd)
Correct answer: subprocess.run(cmd, env={'KEY': 'val'})
The `env` parameter of `subprocess.run()` accepts a dict that completely replaces the inherited environment for the child process.
Question 7: Which Python built-in function converts command-line arguments stored in `sys.argv` into a structured format?
- sys.parse_args()
- argparse.ArgumentParser().parse_args() (Correct answer)
- getopt.getargs()
- click.parse()
Correct answer: argparse.ArgumentParser().parse_args()
`ArgumentParser().parse_args()` reads from `sys.argv[1:]` by default and returns a `Namespace` object with typed, validated attributes.
Which Python module allows you to read and write `.env` files for environment variable management?