I would like to modify the samba configuration file using python. This is my code
from ConfigParser import SafeConfigParser
parser = SafeConfigParser()
parser.read( '/etc/samba/smb.conf' )
for section in parser.sections():
print section
for name, value in parser.items( section ):
print ' %s = %r' % ( name, value )
but the configuration file contains tab, is there any possibility to ignore the tabs?
ConfigParser.ParsingError: File contains parsing errors: /etc/samba/smb.conf
[line 38]: '\tworkgroup = WORKGROUP\n'
Try this:
from StringIO import StringIO
data = StringIO('\n'.join(line.strip() for line in open('/etc/samba/smb.conf')))
parser = SafeConfigParser()
parser.readfp(data)
...
Another way (thank @mgilson for idea):
class stripfile(file):
def readline(self):
return super(FileStripper, self).readline().strip()
parser = SafeConfigParser()
with stripfile('/path/to/file') as f:
parser.readfp(f)