【问题标题】:str not a callable object pythonstr 不是可调用对象 python
【发布时间】:2019-07-10 10:22:10
【问题描述】:

我有点好奇为什么我的代码会导致此错误?
有人可以告诉我一些见解作为解决此问题的方法以及引发此错误的原因吗?

错误:

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()

【问题讨论】:

  • 你忘记了" "之后的*
  • @OlivierMelançon 我在我的其他脚本中使用完全相同的代码并且它没有引发错误为什么它现在才这样做?
  • 好吧,也许在你的其他脚本中永远不会到达错误行

标签: python string object callable


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-09
    • 2015-06-07
    • 2020-09-16
    • 1970-01-01
    • 1970-01-01
    • 2013-08-05
    • 2012-06-04
    • 1970-01-01
    相关资源
    最近更新 更多