場合によっては、Python でフォルダーが存在するかどうかを確認したり、ディレクトリまたはファイルが存在するかどうかを確認したりする必要が重要になります。これは、既存のファイルを上書きしたくない場合や、ロードする前にファイルが利用可能かどうかを確認したい場合があるためです。それ。それで確認するには ディレクトリが例外なく存在するかどうかを確認する方法 Python では、ファイルまたはディレクトリが既に存在するかどうかを確認する次の方法があります。
方法 1: os.path.exists() を使用して Python でファイルまたはディレクトリが存在するかどうかを確認する
OSモジュール Python では、オペレーティング システムと対話するための関数が提供されます。 OS は Python の標準ユーティリティ モジュールに含まれます。このモジュールは、オペレーティング システムに依存する機能を使用するポータブルな方法を提供します。 OSパス モジュールはのサブモジュールです PythonのOSモジュール 共通のパス名の操作に使用されます。
の Python の os.path.exists() メソッド 指定されたパスが存在するかどうかを確認するために使用されます。このメソッドは、指定されたパスが開いているファイル記述子を参照しているかどうかを確認するためにも使用できます。
構文:
os.path.exists(path)>パラメータ:
path: ファイル システム パスを表すパスのようなオブジェクト。パスのようなオブジェクトは、パスを表す文字列またはバイト オブジェクトです。
戻り値の型: このメソッドは bool クラスのブール値を返します。このメソッドは、パスが存在する場合は True を返し、それ以外の場合は False を返します。
例:
os.path.exists() メソッドを説明する Python プログラム
Python3
# importing os module> import> os> # Specify path> path>=> '/usr/local/bin/'> # Check whether the specified> # path exists or not> isExist>=> os.path.exists(path)> print>(isExist)> # Specify path> path>=> '/home/User/Desktop/file.txt'> # Check whether the specified> # path exists or not> isExist>=> os.path.exists(path)> print>(isExist)> |
>
>出力
npm クリーンキャッシュ強制
True False>
方法 2: os.path.isfile() を使用して Python にファイルまたはディレクトリが存在するかどうかを確認する
Python の os.path.isfile() メソッドは、指定されたパスが既存の通常のファイルかどうかを確認するために使用されます。
構文: os.path.isfile(パス)
パラメータ:
path: ファイル システム パスを表すパスのようなオブジェクト。パスのようなオブジェクトは、パスを表す文字列またはバイト オブジェクトです。
戻り値の型: このメソッドは bool クラスのブール値を返します。このメソッドは、指定されたパスが既存の通常ファイルの場合は True を返し、それ以外の場合は False を返します。
例:
os.path.isfile() メソッドを説明する Python プログラム
Python3
# importing os module> import> os> # Path> path>=> 'C:/Users/gfg/Desktop/file.txt'> # Check whether the> # specified path is> # an existing file> isFile>=> os.path.isfile(path)> print>(isFile)> # Path> path>=> '/home/User/Desktop/'> # Check whether the> # specified path is> # an existing file> isFile>=> os.path.isfile(path)> print>(isFile)> |
>
Java バージョン Linux
>
出力:
True False>
方法 3: os.path.isdir() を使用して Python にファイルまたはディレクトリが存在するかどうかを確認する
Python の os.path.isdir() メソッドは、指定されたパスが既存のディレクトリであるかどうかを確認するために使用されます。このメソッドはシンボリック リンクに従います。つまり、指定されたパスがディレクトリを指すシンボリック リンクである場合、メソッドは True を返します。
構文: os.path.isdir(パス)
パラメータ:
path: ファイル システム パスを表すパスのようなオブジェクト。
戻り値の型: このメソッドは bool クラスのブール値を返します。このメソッドは、指定されたパスが既存のディレクトリである場合は True を返し、それ以外の場合は False を返します。
例 1:
os.path.isdir()メソッドを説明するPythonプログラム。
Python3
# importing os.path module> import> os.path> # Path> path>=> '/home/User/Documents/file.txt'> # Check whether the> # specified path is an> # existing directory or not> isdir>=> os.path.isdir(path)> print>(isdir)> # Path> path>=> '/home/User/Documents/'> # Check whether the> # specified path is an> # existing directory or not> isdir>=> os.path.isdir(path)> print>(isdir)> |
>
>
出力:
False True>
例 2:
指定されたパスがシンボリックリンクの場合の os.path.isdir() メソッドを説明する Python プログラム。
Python3
# importing os.path module> import> os.path> # Create a directory> # (in current working directory)> dirname>=> 'GeeksForGeeks'> os.mkdir(dirname)> # Create a symbolic link> # pointing to above directory> symlink_path>=> '/home/User/Desktop/gfg'> os.symlink(dirname, symlink_path)> path>=> dirname> # Now, Check whether the> # specified path is an> # existing directory or not> isdir>=> os.path.isdir(path)> print>(isdir)> path>=> symlink_path> # Check whether the> # specified path (which is a> # symbolic link ) is an> # existing directory or not> isdir>=> os.path.isdir(path)> print>(isdir)> |
>
>
出力:
True True>
方法 4: pathlib.Path.exists() を使用して Python でファイルまたはディレクトリが存在するかどうかを確認する
パスリブモジュール Python では、さまざまなオペレーティング システムに適したセマンティクスを持つファイル システム パスを表すさまざまなクラスが提供されます。このモジュールは、Python の標準ユーティリティ モジュールに含まれます。 Pathlib モジュールのパス クラスは次のように分類されます。 純粋な道 そして コンクリートの道 。純粋なパスは計算操作のみを提供しますが、I/O 操作は提供しません。一方、純粋なパスから継承した具象パスは計算操作と I/O 操作を提供します。
pathlib.Path.exists() メソッドは、指定されたパスが既存のファイルまたはディレクトリを指しているかどうかを確認するために使用されます。
構文: pathlib.Path.exists(パス)
パラメータ:
path: ファイル システム パスを表すパスのようなオブジェクト。
戻り値の型: このメソッドは bool クラスのブール値を返します。このメソッドは、パスが存在する場合は True を返し、それ以外の場合は False を返します。
私のコンピュータの画面サイズはどれくらいですか
例:
Python3
# Import Path class> from> pathlib>import> Path> # Path> path>=> '/home/gfg/Desktop'> # Instantiate the Path class> obj>=> Path(path)> # Check if path points to> # an existing file or directory> print>(obj.exists())> |
>
>
出力:
真実
現在のディレクトリ以下で検索している場合、フォルダーを見つけるには、フォルダー名の前に ./ を使用しないと、間違った結果が得られます。
Python3
import> os> print>(os.path.isdir(>'./my_folder'>))># print true or false if my_folder exist or not in current directory> print>(os.path.isdir(>'./Folder/search_folder'>))>#will tell if search_folder exist or not inside Folder> |
>
均一な混合物
>