Python 将整型变量的值转换为字符串并将其分配给新变量的值变为零
我编写了一个python模块,它获取文件中可用记录的数量,并将其存储到一个变量中。稍后,我需要将该值转换为字符串,并与用户提供的值进行比较。令人惊讶的是,使用python str()函数转换为字符串的int值正在变为零。有人能告诉我如何解决这个问题吗?下面是代码和输出Python 将整型变量的值转换为字符串并将其分配给新变量的值变为零,python,string,Python,String,我编写了一个python模块,它获取文件中可用记录的数量,并将其存储到一个变量中。稍后,我需要将该值转换为字符串,并与用户提供的值进行比较。令人惊讶的是,使用python str()函数转换为字符串的int值正在变为零。有人能告诉我如何解决这个问题吗?下面是代码和输出 def compareFileCount(self, step, path, exp): dirpath = path teststep = step value = exp command = "
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")
输出
在Unix上,os.system的返回值是进程的退出状态,因此0表示进程已无错误退出。您应该使用以下示例:
proc = subprocess.Popen('ls', stdout=subprocess.PIPE)
tmp = proc.stdout.read()
不知道你在这里是什么意思。
os.system
的结果已经是0,因为这是wc
命令的退出代码。理想情况下,变量“operation”保存命令os.system(wc-l4
0
value of operation1 = 0
File record count 0 is not equal to expected value of - 4. Test Step Failed for -teststpe1
proc = subprocess.Popen('ls', stdout=subprocess.PIPE)
tmp = proc.stdout.read()