<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>「設定ファイル」タグの記事一覧Python Tech</title>
	<atom:link href="https://tech.nkhn37.net/tag/%E8%A8%AD%E5%AE%9A%E3%83%95%E3%82%A1%E3%82%A4%E3%83%AB/feed/" rel="self" type="application/rss+xml" />
	<link>https://tech.nkhn37.net</link>
	<description>Python学習サイト</description>
	<lastBuildDate>Wed, 24 Dec 2025 21:17:17 +0000</lastBuildDate>
	<language>ja</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.0</generator>

<image>
	<url>https://tech.nkhn37.net/wp-content/uploads/2021/01/cropped-lion-normal-clear-1-32x32.png</url>
	<title>「設定ファイル」タグの記事一覧Python Tech</title>
	<link>https://tech.nkhn37.net</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>【Python】configparserによる設定ファイル管理</title>
		<link>https://tech.nkhn37.net/python-configparser-basic/</link>
					<comments>https://tech.nkhn37.net/python-configparser-basic/#respond</comments>
		
		<dc:creator><![CDATA[naoki-hn]]></dc:creator>
		<pubDate>Sat, 20 Mar 2021 00:00:00 +0000</pubDate>
				<category><![CDATA[configparser]]></category>
		<category><![CDATA[ConfigParser]]></category>
		<category><![CDATA[read]]></category>
		<category><![CDATA[write]]></category>
		<category><![CDATA[設定ファイル]]></category>
		<guid isPermaLink="false">https://tech.nkhn37.net/?p=1327</guid>

					<description><![CDATA[Python で設定ファイルの管理をするための configparser について解説します。 configparser による設定ファイルの管理 システムを構築する際には、システムの動作を制御する設定情報を設定ファイル [&#8230;]]]></description>
										<content:encoded><![CDATA[
<p class="wp-block-paragraph">Python で設定ファイルの管理をするための <span class="jinr-d--text-color d--marker1 d--bold"><code>configparser</code></span> について解説します。</p>



<h2 class="wp-block-heading jinr-heading d--bold" id="configparserによるconfigファイルの管理"><code>configparser</code> による設定ファイルの管理</h2>



<p class="wp-block-paragraph">システムを構築する際には、システムの動作を制御する設定情報を設定ファイルとして管理することがほとんどです。Python では、設定ファイルを管理するためのモジュールとして <span class="jinr-d--text-color d--marker1 d--bold"><code>configparser</code></span> モジュールが用意されています。</p>



<p class="wp-block-paragraph">この記事では、<code>configparser</code> の基本的な使い方について紹介します。</p>



<section class="wp-block-jinr-blocks-iconbox b--jinr-block b--jinr-iconbox"><div class="d--simple-iconbox6 ">
			<i class="jif jin-ifont-v2books" aria-hidden="true"></i>
			<div class="a--jinr-iconbox">
<p class="wp-block-paragraph">Python の設定ファイル管理では、YAML や TOML ファイルもよく使用されます。以下も参考にしてください。</p>



<ul class="wp-block-list jinr-list">
<li><a href="https://tech.nkhn37.net/python-yaml-basic/" target="_blank" rel="noreferrer noopener">PyYAMLでのYAMLファイル管理</a></li>



<li><a href="https://tech.nkhn37.net/python-tomllib-basic/" target="_blank" rel="noreferrer noopener">tomllibでTOMLファイルを読み込む方法</a></li>
</ul>
</div>
		</div></section>



<h3 class="wp-block-heading jinr-heading d--bold" id="configファイルの形式">設定ファイルの形式</h3>



<p class="wp-block-paragraph"><code>configparser</code> では、Windows で主に使用される INI ファイルに似た構造を扱うことができます。<code>configparser</code> の<a href="https://docs.python.org/ja/3/library/configparser.html?#module-configparser" target="_blank" rel="noreferrer noopener">公式ドキュメント</a>に注釈がありますが、Windows のレジストリ用に拡張された INI 文法はサポートされていません。</p>



<p class="wp-block-paragraph">具体的には、以下のような形式のファイルになります。</p>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">[WEB_SERVER]
host = xxx.xxx.xxx.xxx
port = 80

[DB_SERVER]
host = xxx.xxx.xxx.xxx
port = 3306</pre>



<p class="wp-block-paragraph">設定ファイルの中で <code>[]</code> で囲われている部分をセクションと言い、設定情報の 1 つの区分を表しています。</p>



<p class="wp-block-paragraph">例では、Web サーバーに関するホスト名 (<code>host</code>)、ポート (<code>port</code>) の情報を <code>[WEB_SERVER]</code> のセクションで管理しており、DB サーバーに関するホスト名 (<code>host</code>)、ポート (<code>port</code>) の情報を <code>[DB_SERVER]</code> のセクションで管理しています。</p>



<p class="wp-block-paragraph">セクションや設定値は、任意に増やすことができます。以降では、このような設定ファイルを <code>configparser</code> で具体的に扱う方法について見ていきます。</p>



<h3 class="wp-block-heading jinr-heading d--bold" id="configparserによるconfigファイル管理の基本的な使い方"><code>configparser</code> による設定ファイル管理の基本</h3>



<p class="wp-block-paragraph"><span class="jinr-d--text-color d--marker1 d--bold"><code>configparser</code></span> による設定ファイル管理について例を使って紹介します。</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import configparser

config = configparser.ConfigParser()
config["WEB_SERVER"] = {"HOST": "xxx.xxx.xxx.xxx", "PORT": 80}
config["DB_SERVER"] = {"HOST": "xxx.xxx.xxx.xxx", "PORT": 3306}

# configファイルへの書き込み
with open("config.ini", "w") as config_file:
    config.write(config_file)

# configファイルからの読み込み
config1 = configparser.ConfigParser()
config1.read("config.ini")
# 読み込んだ結果を参照
print(config1["WEB_SERVER"])
print(config1["WEB_SERVER"]["HOST"])
print(config1["WEB_SERVER"]["PORT"])
print(config1["DB_SERVER"])
print(config1["DB_SERVER"]["HOST"])
print(config1["DB_SERVER"]["PORT"])</pre>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">【実行結果】
&lt;Section: WEB_SERVER>
xxx.xxx.xxx.xxx
80
&lt;Section: DB_SERVER>
xxx.xxx.xxx.xxx
3306</pre>



<p class="wp-block-paragraph">例では、設定ファイル情報を <code>config.ini</code> ファイルに書き込み、その後に書き込んだ設定情報を読みこんで表示しています。</p>



<h4 class="wp-block-heading jinr-heading d--bold" id="configファイルの設定と書き込み">設定ファイルの書き込み</h4>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import configparser

config = configparser.ConfigParser()
config["WEB_SERVER"] = {"HOST": "xxx.xxx.xxx.xxx", "PORT": 80}
config["DB_SERVER"] = {"HOST": "xxx.xxx.xxx.xxx", "PORT": 3306}</pre>



<p class="wp-block-paragraph"><code>configparser</code> を使用するには、<code>configparser</code> をインポートし、<span class="jinr-d--text-color d--marker1 d--bold"><code>ConfigParser</code></span> クラスのオブジェクトを作成します。</p>



<p class="wp-block-paragraph"><code>ConfigParser</code> クラスのオブジェクトでは「<code>オブジェクト名["セクション名"] = {キー: 設定値, ...}</code>」というように各セクションの設定値を辞書形式で指定します。</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group=""># configファイルへの書き込み
with open("config.ini", "w") as config_file:
    config.write(config_file)</pre>



<p class="wp-block-paragraph">上記部分は設定ファイルを書き込んでいる部分です。対象ファイルを書き込みモード (<code>"w"</code>)で開いて、<code>ConfigParser</code> クラスの <span class="jinr-d--text-color d--marker1 d--bold"><code>write</code></span> メソッドにファイルオブジェクトを渡すことで設定情報の書き込みができます。</p>



<h4 class="wp-block-heading jinr-heading d--bold" id="configファイルの読み込み">設定ファイルの読み込み</h4>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group=""># configファイルからの読み込み
config1 = configparser.ConfigParser()
config1.read("config.ini")</pre>



<p class="wp-block-paragraph">設定ファイルへ読み込むには、書き込み時と同じように <code>ConfigParse</code> クラスのオブジェクトを生成し、<span class="jinr-d--text-color d--marker1 d--bold"><code>read</code></span> メソッドに読み込む config ファイルのパスを指定します。</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group=""># 読み込んだ結果を参照
print(config1["WEB_SERVER"])
print(config1["WEB_SERVER"]["HOST"])
print(config1["WEB_SERVER"]["PORT"])
print(config1["DB_SERVER"])
print(config1["DB_SERVER"]["HOST"])
print(config1["DB_SERVER"]["PORT"])</pre>



<p class="wp-block-paragraph">読み込んだ結果を設定値を参照する場合には、上記のように「<code>オブジェクト名["セクション名"]["キー"]</code>」といった形で指定することで設定値を参照できます。</p>



<h2 class="wp-block-heading jinr-heading d--bold">まとめ</h2>



<p class="wp-block-paragraph">Python で設定ファイルの管理をするための <span class="jinr-d--text-color d--marker1 d--bold"><code>configparser</code></span> について解説しました。</p>



<p class="wp-block-paragraph">システムを構築する際には、システムの動作を制御する設定情報を設定ファイルとして管理することがほとんどです。<code>configparser</code> は設定ファイルの管理にとても便利ですので、使い方を覚えて活用してみてください。</p>



<section class="wp-block-jinr-blocks-iconbox b--jinr-block b--jinr-iconbox"><div class="d--simple-iconbox6 ">
			<i class="jif jin-ifont-v2books" aria-hidden="true"></i>
			<div class="a--jinr-iconbox">
<p class="wp-block-paragraph"><code>configparse</code>r の公式ドキュメントは<a href="https://docs.python.org/ja/3/library/configparser.html?#module-configparser" target="_blank" rel="noreferrer noopener">こちら</a>を参照してください。</p>
</div>
		</div></section>



<section class="wp-block-jinr-blocks-simplebox b--jinr-block-container"><div class="b--jinr-block b--jinr-box d--heading-box8  "><div class="a--simple-box-title d--bold">ソースコード</div><div class="c--simple-box-inner">
<p class="wp-block-paragraph">上記で紹介しているソースコードについては <a href="https://github.com/nkhn37/python-tech-sample-source/tree/main/python-libraries/configparser" target="_blank" rel="noreferrer noopener">github</a> にて公開しています。参考にしていただければと思います。</p>
</div></div></section>


<section class="b--jinr-block b--jinr-blogcard d--blogcard-hover-up d--blogcard-style1 d--blogcard-mysite t--round "><div class="a--blogcard-label ef">あわせて読みたい</div><a class="o--blogcard-link t--round" href="https://tech.nkhn37.net/python-tech-summary-page/"><div class="c--blogcard-image"><img decoding="async" class="a--blogcard-img-src" width="128" height="72" src="https://tech.nkhn37.net/wp-content/uploads/2024/08/Python-Tech-Pythonプログラミングガイド_new1-640x360.jpg" alt="【Python Tech】プログラミングガイド" /></div><div class="a--blogcard-title d--bold">【Python Tech】プログラミングガイド</div></a></section>]]></content:encoded>
					
					<wfw:commentRss>https://tech.nkhn37.net/python-configparser-basic/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>

<!--
Performance optimized by W3 Total Cache. Learn more: https://www.boldgrid.com/w3-total-cache/?utm_source=w3tc&utm_medium=footer_comment&utm_campaign=free_plugin

Disk: Enhanced  を使用したページ キャッシュ

Served from: tech.nkhn37.net @ 2026-06-26 20:50:06 by W3 Total Cache
-->