Python ファイルが存在するかどうかを確認する(例付き)

⚡ スマートサマリー

Python os.pathモ​​ジュールとpathlibを使用して、ファイルまたはディレクトリが存在するかどうかを確認できます。ここでは、os.path.exists()、os.path.isfile()、os.path.isdir()、およびpathlib.Path.exists()について、それぞれ明確なコード例とともに学習します。

  • exists(): os.path.exists(path) は、ファイルまたはディレクトリが存在する場合に True を返します。
  • 📄 isfile() / isdir(): これらは、それぞれ既存のファイルまたはディレクトリに対してのみTrueを返します。
  • 🐍 pathlib: pathlib.Path(path).exists() は Python 3.4以降。

Python ファイルが存在するか確認する

Python 存在する()

Python 存在する() メソッドは、特定のファイルまたはディレクトリが存在するかどうかを確認するために使用されます。また、パスが開いているファイル記述子を参照しているかどうかを確認するためにも使用されます。ファイルが存在する場合はブール値 true を返し、そうでない場合は false を返します。os モジュールおよび os.path サブモジュールで os.path.exists(path) として使用されます。

この中の Python ファイルが存在するかどうかのチュートリアルでは、ファイル(またはディレクトリ)が存在するかどうかを判断する方法を学びます。 Pythonファイルが存在するかどうかを確認するには Python組み込みライブラリを使用します Python ファイルが存在するかどうかを確認する関数。

ファイルを検証する方法はいくつかあります。 Python 以下にリストされている関数を使用して、ディレクトリが存在するかどうかを確認します。

ファイルが存在するかどうかを確認する方法 Python os.path.exists() を使用する

path.existsを使用すると、ファイルまたはディレクトリが存在するかどうかをすばやく確認できます。手順は次のとおりです。 Python ファイルが存在するかどうかを確認します:

手順 1) os.path モジュールをインポートします。

コードを実行する前に、os.path モジュールをインポートすることが重要です。

import os.path
from os import path

手順 2) path.exists() 関数を使用する

さて、path.exists()関数を使って Python ファイルが存在するかどうかを確認します。

path.exists("guru99.txt")

手順 3) 以下のコードを実行します。

これが完全なコードです

import os.path
from os import path

def main():

   print ("File exists:"+str(path.exists('guru99.txt')))
   print ("File exists:" + str(path.exists('career.guru99.txt')))
   print ("directory exists:" + str(path.exists('myDirectory')))

if __name__== "__main__":
   main()

この例では、ファイル guru99.txt のみが作業ディレクトリに作成されます。

出力:

File exists: True
File exists: False
directory exists: False

Python isfile()

その Python isfile() メソッドは、指定されたパスが既存の通常のファイルかどうかを確認するために使用されます。 特定のパスが既存のファイルの場合はブール値 true を返し、それ以外の場合は false を返します。 os.path.isfile(path) という構文で使用できます。

os.path.isfile()

isfile コマンドを使用すると、指定された入力がファイルかどうかを確認できます。

import os.path
from os import path

def main():

	print ("Is it File?" + str(path.isfile('guru99.txt')))
	print ("Is it File?" + str(path.isfile('myDirectory')))
if __name__== "__main__":
	main()

出力:

Is it File? True
Is it File? False

os.path.isdir()

指定されたパスがディレクトリを指していることを確認したい場合は、os.path.dir() 関数を使用できます。

import os.path
from os import path

def main():

   print ("Is it Directory?" + str(path.isdir('guru99.txt')))
   print ("Is it Directory?" + str(path.isdir('myDirectory')))

if __name__== "__main__":
   main()

出力:

Is it Directory? False
Is it Directory? True

pathlibPath.exists() の場合 Python 3.4

Python 3.4以降のバージョンには、ファイルシステムパスを扱うためのpathlibモジュールがあります。これはオブジェクト指向のアプローチを使用して、 Python フォルダーが存在するかどうかを確認します。

import pathlib
file = pathlib.Path("guru99.txt")
if file.exists ():
    print ("File exist")
else:
    print ("File not exist")

出力:

File exist

完全 Code

これが完全なコードです

import os
from os import path

def main():
    # Print the name of the OS
    print(os.name)
#Check for item existence and type
print("Item exists:" + str(path.exists("guru99.txt")))
print("Item is a file: " + str(path.isfile("guru99.txt")))
print("Item is a directory: " + str(path.isdir("guru99.txt")))

if __name__ == "__main__":
    main()

出力:

Item exists: True
Item is a file: True
Item is a directory: False

よくあるご質問

os.path.exists('file.txt') を使用してください。ファイルまたはディレクトリが存在する場合は True を返し、存在しない場合は False を返します。

os.path.isfile(path) は、パスが既存の通常のファイルである場合にのみ True を返します。

os.path.isdir(path) を使用してください。これは、パスが既存のディレクトリである場合にのみ True を返します。

pathlib.Path('file.txt') を作成し、exists() を呼び出します。ファイルが存在する場合は True を返します (Python 3.4+)。

exists() はファイルとディレクトリに対して True を返しますが、isfile() はファイルに対してのみ True を返します。

GitHub CopilotのようなAIアシスタントは、os.path.exists()とpathlibのチェックを自動補完し、ファイルを開く前に存在ガードを追加します。

はい。AI搭載ツールはos.path.exists()ガードを挿入し、読み書き前にパスをチェックするようにコードをリファクタリングします。

はい。os.path.exists() はファイルとディレクトリの両方に対して True を返します。それらを区別するには、isfile() または isdir() を使用してください。