既に述べたが、具象パスを扱うためのクラスにはPathクラス、PosixPathクラス、WindowsPathクラスの3つがあり、Pathクラスを使用して具象パスのインスタンスを生成することで、実行環境に合わせてPosixPathクラスかWindowsPathクラスのインスタンスが生成される。
ファイルやディレクトリの名前を変更するには、osモジュールではos.rename関数、os.renames関数、os.replace関数を使用する。PathクラスではPath.renameメソッドを使用する(os.replace関数に対応するPath.replace関数もあるが本稿では省略する)。 targetに既存のファイルを ...
Windowsのパス区切り文字は\、MacやLinuxでは/。このOSによる違いは、ファイルパスを扱うプログラムを書く上で、常に悩みの種 ...
A look at the benefits of using pathlib, the "object-oriented way of dealing with paths". Working with files is one of the most common things developers do. After all, you often want to read from ...
CTC教育サービスはコラム「ゼロから歩くPythonの道> 第44回 Pythonの標準モジュールpathlibを使ってパスを取得してみよう」を公開しました。 こんにちは、吉政創成 菱沼です。 今回も「きれいなPythonプログラミング(マイナビ出版)」という書籍を利用して ...
ZIP化の練習でパス選びに苦戦 昨日は復習として、Google Drive上のフォルダ(配下に複数のフォルダとファイルを含む)をZIP化するコードを練習した。 テキストを見ながらClaudeに質問しつつ進めたが、思いのほか手こずった。原因は「同じ処理に複数のやり方がある」ことだった。ZIP化だけでもshutil.make_archiveとzipfile.ZipFileがあり、パスの結合もos.pa ...
At the outset of his PyCon US 2026 talk, Trey Hunner said that his goal was for attendees to stop representing filesystem paths as strings and to use pathlib instead. That's kind of a tall order, at ...
# Import the built-in "pathlib" module. # 3. Create all folders. # 4. Create all files. # Then print: # 1. Whether the "Projects" folder exists. # 2. Whether the "Python" folder exists. # 3. Whether ...