how to open many text files with python
I have 800 text files about protein. Actually I should make a matrix 800 in 800 to compare interaction between proteins. I entered their names in a list. Because writing all names in program is difficult. Now I want to open them in the python programming to use. But I don’t know what the program is to do it.
import csv
from os import listdir
from os.path import isfile,join
Protein_List = [f for f in listdir("/home/rezvane/GENE6") if isfile(join("/home/rezvane/GENE6",f))]
Matrix_Interaction = [[]*7]
Number_of_Interaction = 0
for i in range(7):
CC_Interaction = []
fh = open("/home/rezvane/GENE6/O15209:ZBTB22.txt")
test = False
for line in fh.readline():
if "CC -!- INTERACTION" in line:
test = True
if "CC -!- SUBCELLULAR LOCATION" in line:
break
if test:
data = line.split(";")[0][9:]
CC_Interaction.append(data)
for j in range(7):
开发者_Python百科 if Protein_List[j] in CC_Interaction:
Matrix_Interaction[i][j] = 1
Number_of_Interaction +=1
else:
Matrix_Interaction[i][j] = 0
print Matrix_Interaction
print Number_of_Interaction
Do not write your filenames in code. Instead, do one of the following:
Store your file names in a data store of some sort, like an XML file or database, and use that data store to open your files, or
Write a function that generates the file names based on existing information about the proteins.
Also, consider the possibility that, instead of using separate text files, you should be storing or importing the data into a database, and using that database to analyze and manipulate your protein data instead of text files.
精彩评论