#__author: #日付:2020年2月25日 インポートのConfigParser コンフィグ = configparser.ConfigParser() 設定[ ' DEFAULT ' ] = { ' ServerAliveInterval ':' 45 ' 、 " 圧縮":' はい' 、 " がCompressionLevel ":" 9 " } 設定[ ' bitbucket.org '] = {} コンフィグ[' bitbucket.org ' ] [ " ユーザー" ] = ' 新人' コンフィグ[ ' topsecrect.server.com ' ] = {} topsecret = configの[ ' topsecrect.server.com ' ] topsecret [ ' ホストポート' ] = ' 50022 ' topsecret [ ' ForWardX11 ' ] = ' いいえ' コンフィグ[ ' DEFAULT ' ] [ ' ForWardX11 '] = "はい' (オープンと' config_example.ini '、' W ' たconfigfileとして): config.write(たconfigfile) #1 config.read( 'config_example.ini') プリント(config.sections()) プリント(設定[ ' DEFAULT ' ]) のためのキーで設定[ ' DEFAULT ' ]: プリント(キー、' :'、コンフィグ[ ' DEFAULT ' ] [キー]) プリント(コンフィグ[CONFIG。セクション()[1]]) 印刷(コンフィグ[config.sections()[1] [ ' ホストポート' ]) CONFIG1 = configparser.ConfigParser() プリント(config1.sections()) config1.read(' config_example.ini ' ) プリント(config1.sections( )) プリント(config1.options(' topsecrect.server.com ' )) プリント(config1.get(' DEFAULT '、' ForWardX11 ' )) config1.write(オープン(' config_example2.ini '、' W ' )) config1.add_section(' 新規セクション' ) config1.set(' 新規セクション'、' 新オプション'、' 新しい価値' ) config1.write(オープン(' config_example2.ini '、' W ' )) プリント(config1.sections ()) のための I でconfig1.sections(): プリント(config1.options(i))を config1.remove_option(' 新規セクション'、' 新オプション') config1.remove_section(' 新規セクション' ) config1.set(' bitbucket.org '、' ユーザー'、' 私の名前' ) config1.write(オープン(' config_example2.ini '、' W '))
[ 'bitbucket.org'、 'topsecrect.server.com'] <セクションDEFAULT> serveraliveinterval:45 圧縮:はい がCompressionLevel:9 forwardx11:YES <セクション:topsecrect.server.com> 50022 [] [ 'bitbucket.org' 'topsecrect.server.com'] [ 'ホストポート'、 'forwardx11'、 'serveraliveinterval'、 '圧縮'、 'がCompressionLevel'] はい [ 'bitbucket.org'、 'topsecrect.server.com'、「新規セクション'] ['ユーザ' 'serveraliveinterval'、 '圧縮'、 'がCompressionLevel'、 'forwardx11'] [ 'ホスト・ポート'、 'forwardx11'、 'serveraliveinterval'、'圧縮」、 'がCompressionLevel'] [ '新しいオプション'、 'serveraliveinterval'、 '圧縮'、 'がCompressionLevel'、 'forwardx11'] プロセスは、終了コードを終了0