Can I set a variable for the argument of the with open function in python?

Asked 2 years ago, Updated 2 years ago, 18 views


using python with open('n.text','w')
If so, I would like to write the file instead of n=100, is there a good way?

python

2022-09-29 21:57

1 Answers

Do you mean you want to write a file called 100.text as n=100?

You can combine strings using the + operator.You can also use str to convert numbers into strings.You can use them to do the following:

n=100
with open(str(n)+".text", "w")


2022-09-29 21:57

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.