#!/usr/bin/env python
import os,subprocess,re
f=open("/var/tmp/disks_out","w")
proc=subprocess.Popen(['df', '-h'],stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=False)
out,err=proc.communicate()
for line in out:
f.write(line)
f.close()
f1=open("/var/tmp/disks_out","r")
disks=[]
for line in f1:
m=re.search(r'(c.*s0)',line)
if m:
disk=m.group(1)
disks.append(disk)
disks = disks[0][:-1]
slices =[disks+i for i in str(range(5))]
print(slices)
我正跌到下面的位置:
['c0t5000CCA025A29894d0s0', 'c0t5000CCA025A29894d0s1', 'c0t5000CCA025A29894d0s3', 'c0t5000CCA025A29894d0s4', 'c0t50 00CCA025A29894d0s5', 'c0t5000CCA025A29894d0s6']
但是我也想获得类似的输出:
c0t5000CCA025A29894d0s1,c0t5000CCA025A29894d0s2,c0t5000CCA025A29894d0s3
答案 0 :(得分:0)
如果要用逗号将其获取:
print(','.join(slices))
或者对于python 2.7:
print ','.join(slices)
您打印出的是列表,因此python将其解释为一个。 join()
方法通过传递的字符串连接可迭代的每个元素,更多信息here(对于在标记中的Python 2.7),但似乎您在此处使用的是Python 3。