I need to extract some values out of a file, i wrote the following code.
import os
import sys
rootdir='/home/nsingh/ansible-environments/aws'
for root, subdirs, files in os.walk(rootdir):
for j in subdirs:
print j
mypath=rootdir+'/'+j+'/inventory/group_vars/all'
#print mypath
fo=open(mypath,'r')
f=fo.readlines()
for line in f:
if ('isv_alias' in line or 'LMID' in line or 'products' in line):
path='/home/nsingh/krikCSV_fun.csv'
if('isv_alias' in line):
line=line.strip('isv_alias=')
line= line.strip('"')
elif('LMID' in line):
line=line.strip('LMID=')
else:
line=line.strip('products=')
fi= open(path,'a+')
fi.write(line)
fi.close()
fo.close()
the os.walk method somehow finds a hidden directory as well which is not actually present
loadgen
crapcity
rmstest2
suricatatest
.git
Traceback (most recent call last):
File "testme.py", line 9, in <module>
fo=open(mypath,'r')
IOError: [Errno 2] No such file or directory: '/home/nsingh/ansible-environments/aws/.git/inventory/group_vars/all'
OUTPUT:
: "suricatatest"^M
: suricatatest
: rms_ems_hosted
: 26
: rmstest2
: rms_scl
: 80
: suricatatest
: rms_ems_hosted
: 26
: "suricatatest"^M
: suricatatest
: rms_ems_hosted
: 26
I need the output as & also remove the semicolon:
suricatatest rms_ems_hosted 26