Search code examples
pythonoperating-systemmp3listdir

Check if mp3 file is in given directory


I can't see what i am doing wrong here

import os

def check():
    path = "d://Mu$ic//"
    for file in os.listdir(path):
        if file.endswith("*.mp3"):
            print (os.path.join(path,file))

check()

The output doesn't show up , any idea what am i doing wrong?


Solution

  • Omitting the * should work, since endswith matches on a string suffix (as opposed to *.mp3 which is a wildcard):

    file.endswith(".mp3"):

    Also, you could replace // with just /.