Want to extract data from 1 file and write it to 50 files in python
<p>I want to extract 4 constant columns from a file, and then get a further 'variable' column and write this to a file. I then need this to loop 50 times. So in each of the 50 files there is a different 'variable column'. I have the chunk of code to do this once, but I need to write a loop in order to get the information i need into the 50 files. Here is the code i have:</p>

<pre><code>os.makedirs("new_dir")
directory = os.path.realpath(__file__)
run = directory.split('/')[4]
my_file = open('file_i_am_using.txt', 'r+')
sample = my_file.readline().split('\t')

outputfiles = []
for i in range(51):
    output = 'output%i.txt' %i
    outputfiles.append(open('new_dir/'+output, 'w'))

for line in my_file:
    column = line.split('\t')
    lst = column[1:3]
    lst.insert(2,column[i])
    lst.insert(3,sample[i])
    lst.insert(4,run)
    lst.insert(5,'\n')
    outputfiles[i].write('\t'.join(lst))

for i in range(51):
    outputfiles[i].close()
</code></pre>

<p>At the moment this saves the 50th file with the information required. How can I get the information into the other files? I have tried writing a loop before the 'for line in my_file:' line, but i don't know where I am going wrong?</p>