'str'对象不是可调用的错误

时间:2018-01-30 21:37:04

标签: python python-3.x

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

#!/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'对象不可调用

2 个答案:

答案 0 :(得分:3)

变化:

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

答案 1 :(得分:1)

line.split()是一个返回字符串的函数调用。然后,您尝试将该字符串称为函数,参数为&#34; \ n&#34;。

如果您尝试拆分换行符,则需要

line.split("\n")