Reputation: 95
I need some help. I want to create a python script that create an determinated number of text files with some text, that is incremented by 1 each time. For example, i want to create the text file named 1.px that has in the text " This is the text file number 1", and after that a file named 2.px that has in the text " This is the text file number 2", and so long. I can't really think on how to do it, as my python knowledge is really bad. Thanks for the help.
Upvotes: 2
Views: 439
Reputation: 5110
Run a loop from 1
to n
. In each iteration open a file with the number and write your desired text. And don't forget to close the file when you are done.
n = 10
for i in range(1, n+1):
file = open(str(i) + ".px", "w")
file.write("This is the file number " + str(i))
file.close()
Learn from here how to read from and write to files in python.
Upvotes: 1
Reputation: 1464
Take a look at this site for information on how to create a text file and write content to it.
To auto-increment, you need to loop through whatever numbers you wish using a for
loop. Take a look at the Python documentation to see how to do that.
You can then get the value from the for
loop to write in the file - along with any additional information you want.
Upvotes: 0
Reputation: 7574
This will be enough to get that done!
for i in range(10):
file = open("{}.px".format(i),"w")
file.write("This is file {}".format(i))
Upvotes: 0