I have the following function. The program looks at each file and prints the lines occuring in all 4 files to a new file. I’ve tried file1.close() but I get an error about closing a set? I think I could use a with statement but not sure how to do this, I’m very new to programming.
def secretome():
file1 = set(line.strip() for line in open(path + "goodlistSigP.txt"))
file2 = set(line.strip() for line in open(path + "tmhmmGoodlist.txt"))
file3 = set(line.strip() for line in open(path + "targetpGoodlist.txt"))
file4 = set(line.strip() for line in open(path + "wolfPsortGoodlist.txt"))
newfile = open(path + "secretome_pass.txt", "w")
for line in file1 & file2 & file3 & file4:
if line:
newfile.write(line + '\n')
newfile.close()
I would suggest removing the repetition by extracting your set generation into a function:
Note that you are doing intersection in your code, but you talk about wanting a union, so I used
union()here. There are also a couple of list comprehensions/generator expressions.