Creating a new file, filename contains loop variable, python

前端 未结 5 1163
庸人自扰
庸人自扰 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:45

    Concatenate the i variable to a string as follows:

    f = open("file_"+str(i)+".dat","w")
    

    OR

    f = open("file_"+`i`+".dat","w") # (`i`) - These are backticks, not the quotes.
    

    See here for other techniques available.

提交回复
热议问题