partition and rpartiton getting TypeError: 'builtin_function_or_method' object has no attribute '__getitem__'

StackOverflow https://stackoverflow.com/questions/19525087

  •  01-07-2022
  •  | 
  •  

Question

I'm getting this error when I try to run the script.

TypeError: 'builtin_function_or_method' object has no attribute '__getitem__'

Here is the script:

containerFile = open((tmpImageDirectory+"container.rvbr"), "r")
containedString = containerFile.read()
containerFile.close()
containedFiles = containedString .partition[","]

container.rvbr contains a string with several comas. If execute this str(conatinedString) I get this <type 'str'>, so it's a string.I wonder if somebody can explain this.

Was it helpful?

Solution

Use parentheses rather than square brackets to call a method:

containedFiles = containedArray.partition(",")

Furthermore, you probably want to do split rather than partition: partition will keep the commas in the resulting tuple, even though those commas are not in fact files:

"hello,world".partition(",")
# ('hello', ',', 'world')
"hello,world".split(",")
# ['hello', 'world']

OTHER TIPS

containedFiles = containedString .partition(",")

not

containedFiles = containedString .partition[","]

use parentheses when you want to call a method.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top