2016-03-22 29 views
-2

Değişken "data" cinsinden döndürülmüş değerde kaç satır sayacağım, ancak değişkeni nasıl olduğu gibi kabul etmiyor. ve hata:python hatası: TypeError: 'int' nesnesi yinelenen değil

sum_lines =0 
data=subprocess.call(["nova list"],shell=True) 
for line in data: 
     print line 
     sum_lines += 1 
print sum_lines 

hata

File "./code.py", line 9, in <module> 
    for line in data: 
TypeError: 'int' object is not iterable 

ÇÖZÜM i

data=subprocess.check_output("nova show "+new+"."+projid_new, shell=True) 
for line in data.splitlines(): 
     sum_lines +=1 

cevap

0

subprocess.call check_output kullanmak zorunda() onay o here

Run the command described by args. Wait for command to complete, then return the returncode attribute.

Bu nedenle dönüş değeri yineleme olamaz iterable değil nesne bir tamsayı döndü.

+0

("nova gösterisi "+ yeni + projid_new, kabuk = Doğru".") data.splitlines() kuyruk için : sum_lines + = 1 – Adam

1

Belgelere göre, subprocess.call hata kodunu döndürür.

Run the command described by args. Wait for command to complete, then return the returncode attribute.

'int' object is not iterable durum ile sonuçlanır (bir tam sayı olduğu), geri dönüş kodunu yineleme için çalışıyorum.

Standart çıktı almak için subprocess.check_output'u kullanmanız gerekir. örneğin

Run command with arguments and return its output as a byte string.

çıktı zaten bir dev dizedir ve size dize bölmek için gereken ayrı hatta erişmek için tembel dosya benzeri protokolünü destekleyen olmadığından, str.splitlines() kullanarak.

sum_lines = 0 
data = subprocess.call(["nova list"], shell=True) 
for line in data.splitlines(): 
    print line 
    sum_lines += 1 
print sum_lines 

basitçe satır sayısını saymak için len(data.splitlines()) da çalışacaktır: gibi

Return a list of the lines in the string, breaking at line boundaries. This method uses the universal newlines approach to splitting lines. Line breaks are not included in the resulting list unless keepends is given and true.

Yani kod görünürdü. yaptım ve benim için data = subprocess.check_output çalıştı budur

İlgili konular