350-201(NEW-127Q) · Question #25
Refer to the exhibit. A SOC engineer attempts to automate a recurring log analysis task. Which modification in the Python code must be made to the provided script to automatically save the output to…
The correct answer is A. Add output redirection to save the results to a file: for log in logs: if 'error' in log: with open('output.txt', 'a') as f: f.write(log + '\n'). Option A correctly replaces print(log) with Python's built-in open() function using append mode ('a'), which writes each matching log line to output.txt instead of the console - this is the idiomatic Python way to redirect output to a file. Option B is wrong because cp logs…
Question
Options
- AAdd output redirection to save the results to a file: for log in logs: if 'error' in log: with open('output.txt', 'a') as f: f.write(log + '\n')
- BInclude a copy command to store logs in a different directory before analysis: cp logs /backup
- CStore the output in a file: logs.each { |log| File.open('output.txt', 'a') { |f| f.write(log + "\n") } if log.include?('error') }
- DAdjust the print statement to display the results in the console: for log in logs: if 'error' in log: print('ERROR: ' + log)
How the community answered
(52 responses)- A75% (39)
- B13% (7)
- C4% (2)
- D8% (4)
Explanation
Option A correctly replaces print(log) with Python's built-in open() function using append mode ('a'), which writes each matching log line to output.txt instead of the console - this is the idiomatic Python way to redirect output to a file.
Option B is wrong because cp logs /backup is a Bash shell command, not Python, and it copies files to a backup location rather than writing filtered analysis results anywhere.
Option C is wrong because it uses Ruby syntax (logs.each, block delimiters { }, |log|, log.include?) - it would not run in a Python script at all.
Option D is wrong because it still uses print(), which writes to the console (stdout); adding the 'ERROR: ' prefix changes the format of the display but does not redirect output to a file.
Memory tip: Think "A = Append to a file" - the 'a' mode in open('output.txt', 'a') is the giveaway. Whenever an exam asks about saving Python output, look for open() with f.write() and eliminate any option still using print() or non-Python syntax.
Topics
Community Discussion
No community discussion yet for this question.