nano_exit

基礎的なことこそ、簡単な例が必要だと思うのです。

存在をチェックするpythonコマンドまとめ

  • Path (File and/or Directory)
import os

path = "/test"
os.path.exists( path )  # True or False

path_file = "/test/test.dat"
os.path.isfile( path_file )  # True or False

path_dir = "/test/"
os.path.isdir( path_dir )  # True or False

地味に英語の文章になっていて、"path"が三人称単数であることを受けて、"is"だったり"exist"に三人称単数の"s"が付いたりしている。

  • List
l = [ "test" ]
target = "test"

target in l  # True

try:
    l.index( target )  # integer
except ValueError
    print( "{} does not exist".format( target ) )

l.count( target )  # integer
  • 文字
string = "test"
target = "t"

target in string  # True