将int变量的值转换为字符串并将其分配给新变量将变为零

时间:2016-07-14 12:58:53

标签: python string

我编写了一个python模块,它获取文件中可用记录的数量并将其存储到变量中。稍后我需要将此值转换为字符串并与用户提供的值进行比较。令人惊讶的是,使用python str()函数转换为字符串的int变为零。有人能告诉我如何解决这个问题吗?下面是代码和输出,

def compareFileCount(self, step, path, exp):
    dirpath = path
    teststep = step
    value = exp
    command = "wc -l <"+" "+dirpath
    operation = os.system(command)
    print operation
    operation1=str(operation)
    print "value of operation1 = "+operation1
    if operation1 == value:
            print "File record count "+operation1+" is equal to expected value of - "+value+". Test Step Passed for -"+teststep
            self.logstatus(teststep, "PASSED")
    else:
            print "File record count "+operation1+" is not equal to expected value of - "+value+". Test Step Failed for -"+teststep
            self.logstatus(teststep, "FAILED")

当使用以下值调用上述方法时,

os.compareFileCount("teststpe1", "/data/automation/202585_ForkJoin/testfiles/hvac_feed1.csv", "4")

输出

4
0
value of operation1 = 0
File record count 0 is not equal to expected value of - 4. Test Step Failed for -teststpe1     

1 个答案:

答案 0 :(得分:2)

在Unix上,os.system的返回值是进程的退出状态,因此0表示进程退出时没有错误。你应该使用类似下面的例子:

proc = subprocess.Popen('ls', stdout=subprocess.PIPE)
tmp = proc.stdout.read()