Skip to content

Module 2 challenge: Handling Files :Using Python to Interact with the Operating System (Google IT Automation with Python Professional Certificate) Answers 2025

1. CSV method to write rows
✔️ csv.writerows()
csv.write()
csv.add_rows()
csv.row_writer()

2. Benefit of with open()
✔️ with open() automatically closes your file after it executes
❌ It opens a copy of the file
❌ No difference
❌ Automatically loops through lines

3. File path type
✔️ Absolute
❌ Relative
❌ Relational
❌ None of the above

4. True about file path structure
✔️ The structure of a file path depends on the OS
❌ Must use two backslashes \\
❌ Always the same regardless of OS
❌ File path begins with the file and ends with root

5. Using open() in write mode on existing file
✔️ The old contents will be deleted as soon as the file is opened
❌ Prompt to overwrite or append
❌ Error
❌ Contents added beneath existing content

6. Extra blank lines when reading text file
✔️ Change the print function call to print(line, end="")
❌ Open in binary mode
❌ Skip lines with only whitespace
❌ Use strip() (would remove leading/trailing spaces too)

7. Create a new directory on Linux
✔️ mkdir directory_name
mkfolder
create
newdir

8. Access CSV data by column names
✔️ Use csv.DictReader class
❌ Manually loop through rows
csv.reader() automatically converts
❌ No direct way

9. Verify whether a file exists
✔️ Helps avoid “file not found” errors
❌ Helps find files if you don’t remember name
✔️ Helps prevent overwriting an existing file
❌ Cannot write unless it exists

10. Benefit of registering a CSV dialect
✔️ Allows you to define custom parsing behavior (like removing leading spaces) for CSV files without specifying parameters every time
❌ Simplifies error handling (partial benefit)
❌ Ensures all CSV files use this dialect
❌ Creates a new dialect object (not the primary purpose)


📌 Summary Table

Q# Correct Answer(s)
1 csv.writerows() ✔️
2 with open() auto closes ✔️
3 Absolute ✔️
4 Path depends on OS ✔️
5 Old contents deleted ✔️
6 print(line, end="") ✔️
7 mkdir directory_name ✔️
8 csv.DictReader ✔️
9 Avoid errors, prevent overwriting ✔️
10 Custom parsing behavior ✔️