0
0
Pythonprogramming~10 mins

File modes and access types 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
Bw
Ca
Dr
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'w' which opens the file for writing and erases content.
Using 'a' which opens the file for appending.
2fill in blank
medium

Complete the code to open a file named 'log.txt' for appending new data.

Python
file = open('log.txt', '[1]')
Drag options to blanks, or click blank then click option'
Aa
Bx
Cw
Dr
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'w' which overwrites the file content.
Using 'r' which only reads and does not allow writing.
3fill in blank
hard

Fix the error in the code to open a file for writing only if it does not exist.

Python
file = open('newfile.txt', '[1]')
Drag options to blanks, or click blank then click option'
Ar
Bx
Cw
Da
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'w' which overwrites existing files.
Using 'a' which appends to existing files.
4fill in blank
hard

Fill both blanks to open a file named 'report.txt' for reading and writing.

Python
file = open('report.txt', '[1][2]')
Drag options to blanks, or click blank then click option'
Ar
Bw
C+
Da
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'w+' which truncates the file before writing.
Using 'a+' which opens for appending and reading.
5fill in blank
hard

Fill the blanks to create a dictionary comprehension that maps filenames to their sizes only if size is greater than 1000 bytes.

Python
sizes = {filename: os.path.getsize(filename) for filename in files if os.path.getsize(filename) [1] [2]
Drag options to blanks, or click blank then click option'
A>
B1000
C==
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' which checks for exact size.
Using '<' which checks for smaller sizes.