Python ファイルが存在するかどうかを確認します。 Python でディレクトリが存在するかどうかを確認する方法
Python が存在します()
Python が存在します() メソッドは、特定のファイルまたはディレクトリが存在するかどうかを確認するために使用されます。また、パスが開いているファイル記述子を参照しているかどうかを確認するためにも使用されます。ファイルが存在する場合はブール値 true を返し、そうでない場合は false を返します。 os モジュールと os.path サブモジュールで os.path.exists(path) として使用されます。
この Python ファイルが存在するチュートリアルでは、Python を使用してファイル (またはディレクトリ) が存在するかどうかを判断する方法を学習します。ファイルが Python で存在するかどうかを確認するには、組み込みライブラリ Python のファイルが存在するかどうかを確認する関数を使用します。
以下にリストされている関数を使用して、ディレクトリが存在するかどうかファイルまたは Python チェックを検証するさまざまな方法があります。
- os.path.exists() を使用して Python でファイルが存在するかどうかを確認する方法
- os.path.isfile()
- os.path.isdir()
- pathlibPath.exists()
os.path.exists() を使用して Python でファイルが存在するかどうかを確認する方法
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 のみが作業ディレクトリに作成されます
出力:
ファイルが存在する:True
ファイルが存在する:False
ディレクトリが存在する: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()
出力:
ファイルですか?本当
それはファイルですか?いいえ
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()
出力:
ディレクトリですか? False
ディレクトリですか?真
Python 3.4 の場合は pathlibPath.exists()
Python 3.4 以降のバージョンには、ファイル システム パスを処理するための pathlib モジュールがあります。フォルダーが存在するかどうかを Python でチェックするオブジェクト指向アプローチを使用します。
import pathlib file = pathlib.Path("guru99.txt") if file.exists (): print ("File exist") else: print ("File not exist")
出力:
ファイルが存在します
完全なコード
これが完全なコードです
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()
出力:
アイテムが存在する:True
アイテムがファイルである:True
アイテムがディレクトリである:False
ファイルが存在するかどうかを確認する方法
os.path.exists()
–True
を返します パスまたはディレクトリが存在する場合os.path.isfile()
–True
を返します パスがファイルの場合os.path.isdir()
–True
を返します パスがディレクトリの場合pathlib.Path.exists()
–True
を返します パスまたはディレクトリが存在する場合。 (Python 3.4 以降のバージョン)
Python