string – Python: object of type _io.TextIOWrapper has no len()
string – Python: object of type _io.TextIOWrapper has no len()
The problem occurs due to the way you are opening the text file.
You should add dna = dna.read()
to your code.
so your end code should look something like this:
def mostCommonSubstring():
dna = open(dna.txt, r)
dna = dna.read()
mink = 4
maxk = 9
count = 0
check = 0
answer =
k = mink
while k <= maxk:
for i in range(len(dna)-k+1):
sub = dna[i:i+k]
count = 0
for i in range(len(dna)-k+1):
if dna[i:i+k] == sub:
count = count + 1
if count >= check:
answer = sub
check = count
k=k+1
print(answer)
print(check)
@tfabiant : I suggest this script to read and process a DNA sequence.
To run this code, in the terminal: python readfasta.py fastafile.fasta
import string, sys
##########I. To Load Fasta File##############
file = open(sys.argv[1])
rfile = file.readline()
seqs = {}
##########II. To Make fasta dictionary####
tnv = #temporal name value
while rfile != :
if > in rfile:
tnv = string.strip(rfile)
seqs[tnv] =
else:
seqs[tnv] += string.strip(rfile)
rfile = file.readline()
##############III. To Make Counts########
count_what = [A, T, C, G, ATG]
for s in seqs:
name = s
seq = seqs[s]
print s # to print seq name if you have a multifasta file
for cw in count_what:
print cw, seq.count(cw)# to print counts by seq