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

Viewed 401

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.

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.

Related