I have used biopython 1.72 to read from multiple aligned protein data files. I have extracted some required data from the files to other text files which are then used as input to next function. I need to know if I can create that text file to be a temporary file so that as the script ends the file is also destroyed.
Here is the example code I've used so far:
from Bio import SeqIO
with open ("Example.aln" , 'r') as fo: ### Opening a file from user
list1 = (fo.read().split("\n\n\n")) ### Splitting the file every 3 newline charc
str1 = ''.join(list1[1])
list2 = str1.split("\n\n")
outfile = open ("sample.txt",'w')
outfile.write("This is a text File displaying information about the given sequence file\n\n")
outfile.write(list2[2])
outfile.close()
fo.close() ## Closing the file taken from user
A clustal format file is taken from user and some of the data is extracted to a text file named 'sample.txt'
. This file is then used as an input to another function which opens this text file and performs something else.
What I am asking for is that if I can create this intermediate text file as a temporary file so that it is destroyed as the function exits.
I've tried using the python tempfile Module
for this , but it does not work the way I am looking for.
Can I get any help??
You can create the
sample.txt
file then use :But, if your
.aln
file is not that big, I suggest you to keep it in a dataframe or in an array in pythonNB :
Thankyou for the response. Lemme try this with my file if it works.
The above code will produce multiple records only if you have 3 continuous new line characters in the input files, it will not split the file for "every 3 newline characters", it does not keep a count of how many new line characters it has encountered so far.
If you want something of this sort:
the code above is not the way to go. On the other hand, if your input file is like this:
only then would your code work fine. I'm calling the entities above "entry" and not "line" because they have 1 or 2 new-line characters in them but will be counted as one entry.
Your
fo.close()
line is redundant. When you use awith open() as ..:
context manager, the file is closed automatically when you leave the indented block.This may be why you were getting stuck with
tempfile
, but without seeing the code I can't say for sure.More generally I think you can re-write the above code in to:
I dont think you need to make
fo.read().split()
a tuple generator with the additional( )
but I'm not sure what your input file looks like.Alright, Thanks so much for this help.