Issue
I am trying to go through all my files in a directory and make another text file that will eventually allow me to make plots out of my data. Some of the VariantLine# files do not contain any information in them because those variants were not found in any of my strains. When I start to iterate through my for loop, it says that my list indexing is out of range, but this is happening in the files that don't have anything written in them. I have over 10,000 VariantLine# files, so I don't want to go through each one individually and get rid of all of the ones that had nothing written in them. I just want to parse through the ones that do contain information written in them, since these are the ones that will provide me with the information I need to make plots. The only information I have found thus far has to deal with just skipping a line with no information, not a whole file.
for files in os.listdir("/nobackup/rogers_research/tmiorin/DsantRNAproject"):
if re.search("^VariantLine", files):
filename=files
filenumber=filename[11:]
print filenumber
for line in filename:
stuff=line.split()
strain=stuff[0]
chrom=stuff[1]
posone=stuff[2]
postwo=stuff[3]
Essentially my problem is that I need a way to parse through only the files that have something written in them, so ideally I need to put a line of code before "for line in filename" that will read the files and only continue with the for loop if it actually has stuff printed to it. I can't seem to find any information online, so if anyone happens to know what I could put I would greatly appreciate it. Thanks!
Solution
for files in os.listdir("/nobackup/rogers_research/tmiorin/DsantRNAproject"):
if re.search("^VariantLine", files):
filename=files
filenumber=filename[11:]
print filenumber
for line in filename:
if (not line==""):
stuff=line.split()
strain=stuff[0]
chrom=stuff[1]
posone=stuff[2]
postwo=stuff[3]
if (not line==""): checks whether the line is not empty, and if you wish you can check even line is not equal to "\n" if this don't work
Answered By - Jainil Patel
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.