类型错误:“bool”类型的对象在sys中没有len()。argv长度检查

时间:2021-06-18 20:27:18

What is the reason for the error

这个错误的原因是什么

TypeError: object of type 'bool' has no len()  

Should i import something in my python program? I am using len(sys.argv) in if condition to check for the number of command line arguments, which are float values(like 1.2 and 2.4, etc). Any thoughts? This is in python2.6. This is how i am running the program

我应该在我的python程序中导入一些东西吗?我在if条件中使用len(sys.argv)检查命令行参数的数量,这些参数是浮点值(如1.2和2.4等)。任何想法吗?这是在python2.6。这就是我运行程序的方式

python BeaconsAnalysis.py 2.0 3.0  

And some portion of my code where i am checking this is,

我检查代码的一部分是,

with open("luawrite", "r") as f:
    if(len(sys.argv == 2)):
            for line in f:
                    t1 = sys.argv[1]
                    t2 = sys.argv[2]
                    hashes = line.split()
                    t = hashes[0] 
                    ...........(goes on)

1 个解决方案

#1


15  

You got the parens in the wrong place and need to account for the script name in argv[0]. if(len(sys.argv == 2)): should be if(len(sys.argv) == 3):

您在错误的地方获得了parens,需要在argv[0]中说明脚本名称。如果(len(sys。应该是if(len(sys.argv) == 3):

#1


15  

You got the parens in the wrong place and need to account for the script name in argv[0]. if(len(sys.argv == 2)): should be if(len(sys.argv) == 3):

您在错误的地方获得了parens,需要在argv[0]中说明脚本名称。如果(len(sys。应该是if(len(sys.argv) == 3):