0
0
Pythonprogramming~10 mins

Opening and closing files in Python - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to open a file named 'data.txt' for reading.

Python
file = open('data.txt', '[1]')
Drag options to blanks, or click blank then click option'
Ax
Br
Ca
Dw
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'w' which overwrites the file instead of reading.
Using 'a' which appends to the file instead of reading.
2fill in blank
medium

Complete the code to close the file object named 'file'.

Python
file.[1]()
Drag options to blanks, or click blank then click option'
Aopen
Bread
Cclose
Dwrite
Attempts:
3 left
💡 Hint
Common Mistakes
Calling open() again instead of closing.
Forgetting to close the file causing resource leaks.
3fill in blank
hard

Fix the error in the code to properly open a file for writing.

Python
file = open('output.txt', '[1]')
Drag options to blanks, or click blank then click option'
Arb
Br
Ca
Dw
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'r' mode which is for reading only.
Using 'rb' which is for reading binary files.
4fill in blank
hard

Fill both blanks to open a file for appending and then close it.

Python
file = open('log.txt', '[1]')
file.[2]()
Drag options to blanks, or click blank then click option'
Aa
Bclose
Cread
Dw
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'w' instead of 'a' which overwrites the file.
Forgetting to close the file.
5fill in blank
hard

Fill all three blanks to open a file for reading, read its content, and then close it.

Python
file = open('notes.txt', '[1]')
content = file.[2]()
file.[3]()
Drag options to blanks, or click blank then click option'
Ar
Bread
Cclose
Dw
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'w' mode which is for writing.
Forgetting to close the file after reading.