Python | How to do an infinite addition to a variable?

Made a script that should create 2 files:
a = 1
f = open('text' + str(a+1) + '.txt', 'w')
f = open('text' + str(a+1) + '.txt', 'w')
f.close()
But there is only one, text2.txt
And I have planned: text2.txt and text3.txt
How to do it?
March 23rd 20 at 18:48
3 answers
March 23rd 20 at 18:50
a+=1
Between open tournaments put
Create SINGLE file text3.txt - Guido commented on March 23rd 20 at 18:53
As I understand it, will make the loop adding to a variable and open insert no +1 just below the name was a variable, but a variable was added +1 and so on indefinitely)) I Hope clearly explained - Guido commented on March 23rd 20 at 18:56
March 23rd 20 at 18:52
It can be shorter
! Carefully will generate until you stop

a = 1
while True:
f = open('text' + str(a) + '.txt', 'w')
f.close()
a += 1
Not because you didn't put the code in code tag and all the code is indented evaporated - hulda.Ro commented on March 23rd 20 at 18:55
@hulda.Ro, Yes, it's correct:
a = 1
while True:
 f = open('text' + str(a) + '.txt', 'w')
f.close()
 a += 1
- Ebony.Thompson commented on March 23rd 20 at 18:58
March 23rd 20 at 18:54
I would use the generator
def positive(max=3, start=1):
 while start <= max:
 yield start
 start += 1


for i in positive(3, 1):
 f = open('text' + str(i) + '.txt', 'w')
 f.close()


If you really need an infinite sequence, replace
while start <= max:
on
start=1
while true:
and to remove parameters from functions
but I think the author didn't mean it.

Find more questions by tags Python