There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply () below.
If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.
Join the Discussion. Help a fellow learner on their journey.
Ask or answer a question about this exercise by clicking reply () below!
You can also find further discussion and get answers to your questions over in Language Help.
Agree with a comment or answer? Like () to up-vote the contribution!
with ContextManagerA('fileA.txt', 'a') as A, with ContextManagerB('fileB.txt', 'a') as B:
Correct! Context managers can be nested in one line using a comma and separate with statements
I’ve tested on this lessons exercise and got a syntax error with two ‘with’s’ separated by comma; no error with a single ‘with’. Would it be right to consider that lesson got right explanation, and the quiz has a mistake?
#causes SyntaxError: invalid syntax:
with poem_files('poem.txt', 'r') as poem, with card_files('card.txt', 'w') as card:
print(poem)
print(card)
card.write(poem.read())
#no errors
with poem_files('poem.txt', 'r') as poem, card_files('card.txt', 'w') as card:
print(poem)
print(card)
card.write(poem.read())
I’m not sure where the lesson was going for (the basic nesting or the comma separated arguments) but the syntax is most definitely wrong for that “correct” answer at the moment. I’ll raise it as a bug for sure.