import os, glob
file = os.listdir("my directory: example")
mp3files = list(filter(lambda f: f == '*.txt',file))
print(mp3files)
This code is giving me only : []
import os, glob
file = os.listdir("my directory: example")
mp3files = list(filter(lambda f: f == '*.txt',file))
print(mp3files)
This code is giving me only : []
From Python 3.4 upwards you can use just those two lines for that task:
from pathlib import Path
mp3files = list(Path('.').glob('**/*.txt'))
mp3files = list(filter(lambda f: f.endswith('.txt') ,file))
Should work, since the file name do not match (==
) to *.txt
but rather end with that extension
why don't you use the glob module that you have imported ?
mp3files = glob.glob('*.txt')
this will return a list of all mp3 files in your current working directory. if your files are in a different directory and not in your cwd:
path_to_files_dir = os.path.join(os.getcwd(), 'your_files_dir_name', '*.txt')
mp3files = glob.glob(path_to_files)