Whenever I try to run this code:
#Open file
f = open("i.txt", "r")
line = 1
#Detect start point
def findstart( x ):
length = 0
epsilon = 7
a = 3
line_value = int(f.readline(x))
if line_value == a:
length = length + 1
x = x + 1
findend(x)
elif line_value == epsilon:
x = x + 2
findstart(x)
else:
x = x + 1
findstart(x)
#Detect end point
def findend(x):
line_value = int(f.readline(x))
if line_value == a:
length = length + 1
return ("Accept", length)
elif line_value == epsilon:
x = x + 2
length = length + 2
findend(x)
else:
x = x + 1
length = length + 1
findend(x)
findstart(line)
I get this error code:
Traceback (most recent call last):
File "C:\Users\Brandon\Desktop\DetectSequences.py", line 39, in <module>
findstart(line)
File "C:\Users\Brandon\Desktop\DetectSequences.py", line 16, in findstart
findend(x)
File "C:\Users\Brandon\Desktop\DetectSequences.py", line 26, in findend
line_value = int(f.readline(x))
ValueError: invalid literal for int() with base 10: ''
Can anyone help me to figure out what’s wrong? It seems to me that it’s attempting to read an empty cell but I don’t know why that would be. The file I’m scanning currently only has two lines with each reading “3” so it should output a success but I can’t get past this error.
I’m not sure about your code, but the error message suggests that your file has an empty line in it and you’re trying to convert it to an
int. For example, a lot of text files have an empty line at the end.I’d suggest first checking your line before converting it: