Pythonでパスがファイルなのか確認するよ
※指定したファイルがないと確認できないから注意してね!
パスがファイルなのか確認するよ
# OS関連の機能のモジュールを読み込むよ
import os
# ファイルを指定するよ
targetPath = r"D:\Pythonで作成したフォルダ\あああ.csv"
# パスがファイルかどうかを確認するよ
if os.path.isfile(targetPath):
print(f"『 {targetPath} 』" + "はファイル")
else:
print(f"『 {targetPath} 』" + "はファイルではない")
# フォルダを指定するよ
targetPath = r"D:\Pythonで作成したフォルダ"
# パスがファイルかどうかを確認するよ
if os.path.isfile(targetPath):
print(f"『 {targetPath} 』" + "はファイル")
else:
print(f"『 {targetPath} 』" + "はファイルではない")
実行結果
『 D:\Pythonで作成したフォルダ\あああ.csv 』はファイル 『 D:\Pythonで作成したフォルダ 』はファイルではない
0 件のコメント:
コメントを投稿