SoFunction
Updated on 2024-10-29

Creating multiple files using python loops

Origin of the problem: each line of data in Excel corresponds to put a txt document.

Answer:When writing to a file in python, you have to open the file with open(''), but you can't iterate over variables in quotes " ". I found out that you can put string variables in open(). This solved the problem.

The code is as follows:

i=1
ll=['a','b','c','e','f']
for it in ll:
 i_str=str(i)
 filename=i_str+'.txt'
 f=open( filename,'w')

 ("something")
 ()
 i=i+1

The above method of creating multiple files using python loop is all that I have shared with you.