0
0
Pythonprogramming~20 mins

Handling multiple resources in Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Master of Handling Multiple Resources
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of nested with statements
What is the output of this Python code that opens two files using nested with statements?
Python
with open('file1.txt', 'w') as f1:
    f1.write('Hello')
with open('file2.txt', 'w') as f2:
    f2.write('World')

with open('file1.txt', 'r') as f1:
    with open('file2.txt', 'r') as f2:
        print(f1.read() + ' ' + f2.read())
ASyntaxError
BHelloWorld
CHello World
DWorld Hello
Attempts:
2 left
💡 Hint
Remember that the files are opened and read in the order of the nested with statements.
Predict Output
intermediate
2:00remaining
Using multiple context managers in one with statement
What will this code print when opening two files in a single with statement?
Python
with open('file1.txt', 'w') as f1, open('file2.txt', 'w') as f2:
    f1.write('Data1')
    f2.write('Data2')

with open('file1.txt', 'r') as f1, open('file2.txt', 'r') as f2:
    print(f1.read() + '-' + f2.read())
ATypeError
BData1-Data2
CData1Data2
DData2-Data1
Attempts:
2 left
💡 Hint
Multiple context managers can be opened in one with statement separated by commas.
🔧 Debug
advanced
2:00remaining
Identify the error in resource handling
What error does this code raise when trying to open two files using nested with statements incorrectly?
Python
with open('file1.txt', 'r') as f1:
    with open('file2.txt', 'r') as f2:
        print(f1.read() + f2.read())
ATypeError
BFileNotFoundError
CIndentationError
DSyntaxError
Attempts:
2 left
💡 Hint
Check the syntax of the with statement lines carefully.
Predict Output
advanced
2:00remaining
Effect of exception inside multiple with statement
What will be printed by this code that opens two files and raises an exception inside the with block?
Python
try:
    with open('file1.txt', 'w') as f1, open('file2.txt', 'w') as f2:
        f1.write('Test')
        raise ValueError('Oops')
        f2.write('Fail')
except ValueError as e:
    print('Caught:', e)

with open('file1.txt', 'r') as f1:
    print(f1.read())
A
Caught: Oops
Test
B
Caught: Oops
C
Test
Caught: Oops
DTest
Attempts:
2 left
💡 Hint
The exception stops execution inside the with block but the file is still written and closed properly.
🧠 Conceptual
expert
2:00remaining
Order of resource release in multiple context managers
When using multiple context managers in one with statement like with A() as a, B() as b:, in which order are the resources released (their __exit__ methods called)?
AFirst B's __exit__ then A's __exit__
BFirst A's __exit__ then B's __exit__
CBoth __exit__ methods are called simultaneously
DOrder is undefined and depends on Python version
Attempts:
2 left
💡 Hint
Think about how nested with statements work and the stack of context managers.