I would like to accept a certain number (limited to - say, 3) to evaluate a condition within the a with
statement. I did find it's possible to do something like this. Can I do something similar to the following in the with
statement by evaluating a condition:
def foo(num):
with open("1.txt","a") as f1, open("2.txt") as f2 <<if num >= 2>>, open("3.txt") as f3 <<if num >= 3>>:
if num >= 1:
f1.write("1")
if num >= 2:
f2.write("2")
if num >= 3:
f3.write("3")
Is this possible? Is there any other way I can evaluate an if condition for the objects involved in a with
statement so that I use exactly the same number of objects as in num
?
P.S. : This code above is just an example using files, it can be replaced by objects too.