2024.07.25(更新日: 2024.07.25)
os.path.existsでファイルとディレクトリの存在確認をしてみた

はじめに
os.path.exists(‘A’)で、Aがあるかどうか確認できる。
フォルダ構成
以下のようなフォルダ構成。

exists.pyに書いた内容と出力結果


ソースコードの解説
1行目でosモジュールをインポートしている。
3行目では、os.path.existsを使用して、sample.txtの存在確認をしている。
4行目では、sampleフォルダの存在確認をしている。
参考サイト
こちらのサイトを参考にさせてもらいました。

投稿ID : 23079
コメントを残す