configParser module

# __author:
# date:2020/2/25
import configparser
config = configparser.ConfigParser()
config['DEFAULT'] = {'ServerAliveInterval' : '45',
                     "Compression" : 'yes',
                     "CompressionLevel" : "9"
                     }
config['bitbucket.org'] = {}
config['bitbucket.org']["User"] = 'New Comer'
config['topsecrect.server.com'] = {}
topsecret = config['topsecrect.server.com']
topsecret['Host Port'] = '50022'
topsecret['ForWardX11'] = 'no'
config['DEFAULT']['ForWardX11'] = 'yes'
with open('config_example.ini', 'w') as configfile:
    config.write(configfile)
# config.read('config_example.ini')
print(config.sections())
print(config['DEFAULT'])
for key in config['DEFAULT']:
    print(key,':',config['DEFAULT'][key])
print(config[config.sections()[1]])
print(config[config.sections()[1]]['Host Port'])
config1 = configparser.ConfigParser()
print(config1.sections())
config1.read('config_example.ini')
print(config1.sections())
print(config1.options('topsecrect.server.com'))
print(config1.get('DEFAULT','ForWardX11'))
config1.write(open('config_example2.ini', 'w'))
config1.add_section('New Section')
config1.set('New Section','New Option','New Value')
config1.write(open('config_example2.ini', 'w'))
print(config1.sections())
for i in config1.sections():
    print(config1.options(i))
config1.remove_option('New Section','New Option')
config1.remove_section('New Section')
config1.set('bitbucket.org','User','My Name')
config1.write(open('config_example2.ini','w'))
 
['bitbucket.org', 'topsecrect.server.com']
<Section: DEFAULT>
serveraliveinterval : 45
compression : yes
compressionlevel : 9
forwardx11 : yes
<Section: topsecrect.server.com>
50022
[]
['bitbucket.org', 'topsecrect.server.com']
['host port', 'forwardx11', 'serveraliveinterval', 'compression', 'compressionlevel']
yes
['bitbucket.org', 'topsecrect.server.com', 'New Section']
['user', 'serveraliveinterval', 'compression', 'compressionlevel', 'forwardx11']
['host port', 'forwardx11', 'serveraliveinterval', 'compression', 'compressionlevel']
['new option', 'serveraliveinterval', 'compression', 'compressionlevel', 'forwardx11']

Process finished with exit code 0
 

  

 

猜你喜欢

转载自www.cnblogs.com/plkm/p/12363144.html