Writing to Multiple Files
<p>I'm sure many of us are familiar with the python file IO pattern to write data to an outfile, as seen below;</p>

<pre><code>outfile = open("myFile", "w")
data.dump()
outfile.close()
</code></pre>

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

<p>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:</p>

<pre><code>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
        ?...
</code></pre>

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

<pre><code>&gt;&gt;&gt; my_list = []
&gt;&gt;&gt; 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)
... 
file100.txt
file200.txt
file300.txt
file400.txt
file500.txt
file600.txt
file700.txt
file800.txt
file900.txt
file1000.txt
</code></pre>

<p>Fill in the blanks, but a simple string concatenation would do the trick.</p>

python file-io