Typhon Typhon - 1 year ago 85
Python Question

Writing to Multiple Files

I'm sure many of us are familiar with the python file IO pattern to write data to an outfile, as seen below;

outfile = open("myFile", "w")

This is all supposing that we already have the file and can access it. Now, I want to create something a bit more complicated.

Say I have a for-loop that iterates through an integer i. Say the range is 1 - 1000, and for the sake of saving space and headaches I want to write data I have been saving up in my loop every time that i % 100 == 0. This should write ten files:

for i in range (1, 1001):
#scrape data from API and store in some structure
if i % 100 == 0:
#Create a new outfile, open it, and write the data to it

How would I go about, in python, automatically creating new files with unique names, and opening and closing them for writing data?

Answer Source
>>> my_list = []
>>> for i in range(1, 1001):
...     # do something with the data... in this case, simply appending i to a list
...     my_list.append(i)
...     if i % 100 == 0:
...         # create a new file name... it's that easy!
...         file = fname + str(i) + ".txt"
...         # create the new file with "w+" as open it
...         with open(file, "w+") as f:
...             for item in my_list:
...                 # write each element in my_list to file
...                 f.write("%s" % str(item))
...         print(file)

Fill in the blanks, but a simple string concatenation would do the trick.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download