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
Use f = open("file_{0}.dat".format(i),'w'). Actually, you might want to use something like f = open("file_{0:02d}.dat".format(i),'w'), which will zero-pad the name to keep it at two digits (so you get "file_01" instead of "file_1", which can be nice for sorting later). See the documentation.