0
0
Pythonprogramming~10 mins

Handling large files efficiently 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 large file for reading.

Python
with open('large_file.txt', [1]) as file:
    pass
Drag options to blanks, or click blank then click option'
A'r'
B'w'
C'a'
D'x'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'w' or 'a' when you want to read the file.
2fill in blank
medium

Complete the code to read a large file line by line efficiently.

Python
with open('large_file.txt', 'r') as file:
    for [1] in file:
        print(line.strip())
Drag options to blanks, or click blank then click option'
Atext
Bline
Cchar
Dword
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'char' or 'word' which do not represent lines.
3fill in blank
hard

Fix the error in the code to read a large file in chunks.

Python
with open('large_file.txt', 'r') as file:
    while True:
        chunk = file.read([1])
        if not chunk:
            break
        print(chunk)
Drag options to blanks, or click blank then click option'
ANone
B'1024'
C1024
D''
Attempts:
3 left
💡 Hint
Common Mistakes
Passing a string instead of an integer to read().
4fill in blank
hard

Fill both blanks to create a dictionary with word lengths for words longer than 3 characters.

Python
words = ['apple', 'bat', 'carrot', 'dog']
lengths = {word: [1] for word in words if len(word) [2] 3}
Drag options to blanks, or click blank then click option'
Alen(word)
B>
C<
D==
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' or '==' which filter wrong words.
5fill in blank
hard

Fill all three blanks to create a dictionary with uppercase keys and the original words as values for words longer than 4 characters.

Python
words = ['apple', 'bat', 'carrot', 'dog']
result = { [1]: [2] for word in words if len(word) [3] 4 }
Drag options to blanks, or click blank then click option'
Aword.upper()
Bword
C>
Dlen(word)
Attempts:
3 left
💡 Hint
Common Mistakes
Using len(word) as value or wrong comparison operators.