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

第3章 コマンドライン コマンドラインで作業する

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

コマンドラインは、ゴムに当たるところだ。グラフィカル・インタフェースを備えた強力なツールは数多くあるが、DevOps作業ではコマンドラインが依然としてホームだ。Python内からShell環境にアクセスすることと、Pythonコマンドラインツールを作成することは、DevOpsのためにPythonを使用する際に必要なことだ。

シェルを使う

Pythonはシステムやシェルとやりとりするためのツールを提供している。sysossubprocess の各モジュールは必須ツールなので、使いこなすべきである。

sysモジュールでインタプリタと話す

sys モジュールは、Pythonインタプリタと密接に結びついた変数やメソッドへのアクセスを提供する。

読み取り時のバイトの解釈には2つの方法がある。1つ目は、リトルエンディアンで、後続の各バイトをより大きな意味を持つ(より大きな桁を表す)と解釈する。もう1つは、ビッグエンディアンで、最初のバイトが最大の意味を持ち、そこから下に移動すると仮定する。

sys.byteorder 属性を使えば、現在のアーキテクチャのバイトオーダーを見ることができる:

In [1]: import sys

In [2]: sys.byteorder
Out[2]: 'little'

sys.getsizeof を使えば、Pythonオブジェクトのサイズを見ることができる。これは限られたメモリを扱う場合に便利だ:

In [3]: sys.getsizeof(1)
Out[3]: 28

基礎となるオペレーティングシステムによる違いによって異なる振る舞いをしたい場合は、sys.platform

In [5]: sys.platform
Out[5]: 'darwin'

より一般的な状況は、Pythonの特定のバージョンでしか利用できない言語機能やモジュールを使いたい場合である。実行中のPythonインタプリタに基づいて振る舞いを制御するために、sys.version_info 。ここでは、Python 3.7、3.7より下のPythonバージョン3、3より下のPythonバージョンに対して異なるメッセージを表示する:

if sys.version_info.major < 3:
    print("You need to update your Python version")
elif sys.version_info.minor < 7:
    print("You are not running the latest version of Python")
else:
    print("All is good.")

sys の使い方については、この章の後半でコマンドラインツールを書くときに詳しく説明する。

osモジュールを使ってオペレーティングシステムを扱う

第2章で、ファイルシステムを扱うためにos 。このモジュールには、オペレーティング・システムを扱うための様々な属性や関数も含まれている。例3-1ではそのいくつかを紹介する。

例3-1. osモジュールの例
In [1]: import os

In [2]: os.getcwd() 
Out[2]: '/Users/kbehrman/Google-Drive/projects/python-devops ...
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