I don't know if this is the best way of doing it, but this would be my method:

1) Read the data from the file. If the files are very long, then perhaps loop this process and do it in small chunks.
i.e.

Code:

file = open("file_test.txt", "r")
file_data = file.read()

2) Then split each line of the file.
i.e.

Code:

line_splits = file_data.split('\n')

3) Then for each element of line_splits, you will have to split again based on spaces. You know that the first element of every line is of the form: geneXX so you can easily turn that into a file name and open a new file called that. As for the rest of it, you will have to match that up with other file (probably using splits again).

So basically if you add all that together you will probably have something along the lines of:

Code:

file = open("file_test.txt", "r")
file_data = file.read()
file.close()
# Split the file based on new lines
data_splits = file_data.split('\n')
for i in range(len(data_splits)):
# For each line, split again based on spaces
line_splits = data_splits[i].split(' ')
# Now you know the first element in line_splits is the new file name
out_file = open(line_splits[0] + '.txt', 'w')
for j in range(1, len(line_splits)):
# Now match each element of this list with your other file and add them all
# to out_file.
out_file.close()

That should give you a rough idea of 1 way of skinning this cat. Go do the rest.