Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

range() function produces error 'TypeError: 'str' object is not callable'

My problem is the following line of code:

zreal_zimg_dfs = [pd.concat([zreal_dfs[i], zimg_dfs[i]], axis = 1) for i in range(len(filestrings))]

The filestrings variables are taken from a directory if they match a certain form. They are then sorted and collected as an array of strings with this code:

#Setting up die cast adress-string we use with glob
string = str(DATA_DIR.joinpath('Checkup_*_NMC_{nmc_nr}_*EIS000{EIS_nr}.csv'.format(nmc_nr = nmc_nr, EIS_nr = EIS_nr)))

#Grab all the strings in the folder that fit our die
filestrings = [name for name in glob.glob(string)]
filestrings.sort(key = natural_keys)

I'm going crazy beause this has worked before but now I get the error

TypeError: 'str' object is not callable

I printed out statement wise and found the culprit was this

  print(range(len(filestrings)))
TypeError: 'str' object is not callable

while

print(len(filestrings))

simply outputs 24, no questions asked.

Please help. When things like this stop progress programming is at its worst.

like image 895
lpnorm Avatar asked May 08 '26 15:05

lpnorm


1 Answers

Is it possible you've shadowed range or len by accident by assigning a string to a variable with either name? You should avoid using any of the built-ins as object names.

like image 110
crcvd Avatar answered May 10 '26 04:05

crcvd



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!