【问题标题】:'str' object is not callable error“str”对象不可调用错误
【发布时间】:2018-01-30 21:37:04
【问题描述】:

如何修复下面代码中的运行时错误?

#!/usr/bin/env python3
import zipfile
def extract(zipf, pwd):
    try:
        zipf.extractall(psw=(pwd))
        return
    except :
        return
def main():
    zipf = zipfile.is_zipfile("dave.zip")
    pwdfile=open("pwdlist.txt", "r")
    for line in pwdfile.readlines():
        psw = line.strip()("\n")
        guess = extract(zipf, psw)
        if guess:
            print ("[+] Password is : %s"%(psw) + ("\n"))
            exit(0)
if __name__ == '__main__':
    main()

错误:

psw = line.strip()("\n") TypeError: 'str' object is not callable

【问题讨论】:

  • 你的意思是psw = line.strip("\n")

标签: python python-3.x


【解决方案1】:

变化:

psw = line.strip()("\n") 

psw = line.strip("\n")

注意,line.strip() 返回一个字符串,然后您尝试调用它,所以它类似于:

>>> "I'm a string"()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'str' object is not callable

或许更清楚:

>>> string_object = "I'm a string"
>>> string_object()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'str' object is not callable

【讨论】:

    【解决方案2】:

    line.split() 是一个返回字符串的函数调用。然后,您尝试将该字符串 作为函数调用,参数为“\n”。

    如果你想在换行符处拆分,你需要

    line.split("\n")
    

    【讨论】:

    • “如果你想在换行符处拆分,你需要line.strip("\n") - 或者line.splitlines() 可以。如果需要,您还可以使用 str.splitlines 保存换行符。只是一个旁注。
    • strip == remove smth from begin/end of string, split = 从一个字符串中创建一个字符串列表 - 你的回答让我很困惑。
    • 哦......呃......手指做错了......谢谢。答案已编辑。投赞成票的人一定是读懂了我的想法,而不是原文。
    猜你喜欢
    • 1970-01-01
    • 2018-04-24
    • 1970-01-01
    • 1970-01-01
    • 2021-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多