Complete the code to run a subprocess command that lists files.
import subprocess def test_list_files(): result = subprocess.run(['ls', '[1]'], capture_output=True, text=True) assert result.returncode == 0
The -a option lists all files including hidden ones. It is a common flag for ls.
Complete the code to check that the subprocess output contains the word 'test'.
import subprocess def test_output_contains_test(): result = subprocess.run(['echo', 'this is a test'], capture_output=True, text=True) assert [1] in result.stdout
The output contains the exact word 'test' in lowercase, so the assertion checks for 'test'.
Fix the error in the subprocess call to correctly capture output as text.
import subprocess def test_capture_text(): result = subprocess.run(['echo', 'hello'], [1]=True, capture_output=True) assert result.stdout.strip() == 'hello'
text=True in older Python versions where it is unsupported.The universal_newlines=True argument makes stdout a string instead of bytes in Python 3.7+. Alternatively, text=True can be used in newer versions.
Fill both blanks to run a subprocess that checks Python version and asserts success.
import subprocess def test_python_version(): result = subprocess.run(['[1]', '[2]'], capture_output=True, text=True) assert result.returncode == 0
python3 when only python is available.The command python -V prints the Python version and returns 0 on success.
Fill all three blanks to create a dictionary comprehension filtering files ending with '.py'.
import subprocess def test_list_python_files(): result = subprocess.run(['ls'], capture_output=True, text=True) files = { [1]: True for [2] in result.stdout.splitlines() if [3].endswith('.py') } assert 'test_script.py' in files
Using the same variable f for key, loop variable, and condition is correct and consistent.