Skip to Content
DevOpsのためのPython
book

DevOpsのためのPython

by Noah Gift, Kennedy Behrman, Alfredo Deza, Grig Gheorghiu
March 2025
Intermediate to advanced
506 pages
8h 3m
Japanese
O'Reilly Media, Inc.
Content preview from DevOpsのためのPython

第2章 ファイルとファイルシステムの自動化 ファイルとファイルシステムを自動化する

この作品はAIを使って翻訳されている。ご意見、ご感想をお待ちしている:translation-feedback@oreilly.com

Pythonの最も強力な機能の1つは、テキストとファイルを操作する能力だ。DevOpsの世界では、アプリケーションログの検索であれ構成ファイルの伝播であれ、ファイル内のテキストを絶えず解析、検索、変更している。ファイルは、データ、コード、設定の状態を永続化する手段であり、ログで何が起こったかを振り返る方法であり、設定で何が起こるかを制御する方法だ。 Pythonを使えば、繰り返し使用できるファイルやコード内のテキストを作成、読み取り、変更することができる。これらのタスクを自動化することは、まさに現代のDevOpsを従来のシステム管理から切り離す一面である。手動で従わなければならない指示のセットを保持するのではなく、コードを書くことができる。これによって、手順を見落としたり、順番を間違えたりする可能性を減らすことができる。システムが実行するたびに同じステップを使用していると確信が持てれば、プロセスに対する理解と信頼が深まる。

ファイルの読み書き

open 関数を使えば、ファイルの読み書きができるファイル・オブジェクトを作成できる。引数は2つで、ファイルのパスとモード(オプション引数のデフォルトは読み取り)である。モードは、ファイルの読み書き、テキスト・データかバイナリ・データかを指定するために使用する。モードrを使ってテキスト・ファイルを開き、その内容を読み取ることができる。ファイル・オブジェクトには、ファイルの内容を文字列として返す メソッドがある:read

In [1]: file_path = 'bookofdreams.txt'
In [2]: open_file = open(file_path, 'r')
In [3]: text = open_file.read()
In [4]: len(text)
Out[4]: 476909

In [5]: text[56]
Out[5]: 's'

In [6]: open_file
Out[6]: <_io.TextIOWrapper name='bookofdreams.txt' mode='r' encoding='UTF-8'>

In [7]: open_file.close()

ファイルを使い終わったら閉じるのが良い習慣だ。Pythonはファイルがスコープから外れるとファイルを閉じるが、それまではファイルはリソースを消費し、他のプロセスがそれを開くのを妨げるかもしれない。

readlines メソッドを使ってファイルを読み取ることもできる。このメソッドはファイルを読み取り、その内容を改行文字で分割する。文字列のリストを返す。各文字列は元のテキストの1行である:

In [8]: open_file = open(file_path, 'r')
In [9]: text = open_file.readlines()
In [10]: len(text)
Out[10]: 8796

In [11]: text[100]
Out[11]: 'science, when it admits the possibility of occasional hallucinations\n'

In [12]: open_file.close()

ファイルを開く便利な方法は、

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

FastAPIで生成型AIサービスを作る

FastAPIで生成型AIサービスを作る

Alireza Parandeh

Publisher Resources

ISBN: 9798341625402