首页 > 解决方案 > str 不是可调用对象 python

问题描述

我有点好奇为什么我的代码会导致这个错误?
有人可以向我展示一些见解作为解决此问题的方法以及提高它的原因吗?

错误:

chunk += " " (16 - len(chunk) % 16)
TypeError: 'str' object is not callable

我的代码:

def upload(item):
    with open(item, "rb") as fp:
        while True:
            chunk = fp.read(64*1024)

            if len(chunk) == 0:
                break
            elif len(chunk) % 16 != 0:
                chunk += " " (16 - len(chunk) % 16)

            self.s.send(encrypt(self.key, chunk, self.iv))

    self.s.send("DONE")
    self.update()

标签: pythonstringobjectcallable

解决方案


更改chunk += " " (16 - len(chunk) % 16)为:

 chunk += " " * (16 - len(chunk) % 16)

如果你什么都不放在那里,这意味着它" "是可调用的,你正试图用16 - len(chunk) % 16参数调用它。


推荐阅读