Creating a new file, filename contains loop variable, python

前端 未结 5 1170
庸人自扰
庸人自扰 2020-11-27 17:10

I want to run a function over a loop and I want to store the outputs in different files, such that the filename contains the loop variable. Here is an example



        
5条回答
  •  囚心锁ツ
    2020-11-27 17:57

    Try this:

    for i in xrange(10):
       with open('file_{0}.dat'.format(i),'w') as f:
           f.write(str(func(i)))
    

提交回复
热议问题