I have a number of text files with a single long hex number inside each file. I want to find out the length of each hex number, i.e. ['FFFF0F'] =6, ['A23000000000000FD'] =17.
i read the file in:
file_to_open = open(myFile , 'r')
filey = file_to_open.readlines()
print(type(filey))
a = hex(int(filey, 16))
print(type(a))
n = len(filey)
print('length = ', n)
And my error is:
TypeError: int() cannot convert non-string with explicit base
if I remove the base 16 I get the error:
TypeError : int() argument must be a string, a bytes-like object or a number, not 'list'
Any ideas on how to just read in the number and find how many hex digits it contains?
readlines
returns list
of str
s (lines) - in case of one-line file it is list with one element. Use read
to get whole text as single str
, strip
leading and trailing whitespaces, then just get len
:
with open(myFile , 'r') as f:
filey = f.read()
filey = filey.strip()
n = len(filey)
Note also that I used with
so I do not have to care about closing that file handle myself. I assume all your files are single-line and contain some hex number. Note that if your number has any leading 0
s, they will be counted too, so for example length of 000F
is 4.