わかさぎのブログ

プログラミング、Atcoderの勉強とか

startswithとendswith

def xlsx_delete(path):
#紛れ込んでいるエクセルファイルを削除
    delete_list=[]
    for file in path:
        if file.endswith("xlsx"):
            delete_list.append(file)
        else:
            pass
    for file in delete_list:
        os.remove(file)
        path.remove(file)

(文字列).endswith()として使って、返り値はブーリアン

 

解説

ファイル名が格納されたリストを用いて、各要素の末尾がなんの文字列で終わっているかを判定する。