0
0
Pythonprogramming~10 mins

File system interaction basics 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' in read mode.

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 overwrites the file.
Using 'a' which appends instead of reading.
2fill in blank
medium

Complete the code to write the string 'Hello' to a file named 'output.txt'.

Python
with open('output.txt', '[1]') as f:
    f.write('Hello')
Drag options to blanks, or click blank then click option'
Aw
Br
Ca
Drb
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'r' which is read-only.
Using 'rb' which is read binary mode.
3fill in blank
hard

Fix the error in the code to read all lines from 'log.txt' into a list.

Python
with open('log.txt', 'r') as file:
    lines = file.[1]()
Drag options to blanks, or click blank then click option'
Areadlines
Bread
Creadline
Dwrite
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'readline' which reads only one line.
Using 'write' which is for writing, not reading.
4fill in blank
hard

Fill both blanks to create a dictionary with filenames as keys and their sizes in bytes as values.

Python
import os
files = ['a.txt', 'b.txt', 'c.txt']
sizes = { [1] : os.path.[2](f) for f in files }
Drag options to blanks, or click blank then click option'
Af
Bgetsize
Cfiles
Dexists
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'files' as key which is the whole list.
Using 'exists' which checks if file exists, not size.
5fill in blank
hard

Fill all three blanks to filter files larger than 1000 bytes and create a dictionary with uppercase filenames as keys and sizes as values.

Python
import os
files = ['x.log', 'y.log', 'z.log']
large_files = { [1] : [2] for f in files if os.path.getsize(f) [3] 1000 }
Drag options to blanks, or click blank then click option'
Af.upper()
Bos.path.getsize(f)
C>
Df.lower()
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' in filter.
Using 'f.lower()' instead of uppercase.
Using wrong function for size.