getting file list using glob in python getting file list using glob in python python-3.x python-3.x

getting file list using glob in python


Looks like you just need to include your slash to search in the correct directory.

import globmydir = "C:\Data"file_list = glob.glob(mydir + "/*.csv") # Include slash or it will search in the wrong directory!!print('file_list {}'.format(file_list))


Try fnmatch:

import osfrom fnmatch import fnmatchmydir = "C:/Data"file_list = [file for file in os.listdir(mydir) if fnmatch(file, '*.csv')]print('file_list {}'.format(file_list))

Also, use RegEx:

import osimport remydir = "C:/Data"file_list = [file for file in os.listdir(mydir) if re.search('.*\.png', file)]  print('file_list {}'.format(file_list))

By the way, glob is a module, you should use glob.glob() like this:

from glob import globmydir = "C:/Data"file_list = glob(mydir + "/*.csv")print('file_list {}'.format(file_list))


You are missing a backslash between the filename and the directory. BUT you can't end a string with a single backslash, because it will think you are trying to escape the endquote. You can put it in the call to glob instead. (Note, that you'll want to make both strings raw, it's good practice in case your directory starts with an "n" or a "t", which in this case is interpreted as spacing characters.):

import globmydir = r"C:\Data"file_list = glob.glob(mydir + r"\*.csv")print('file_list {}'.format(file_list))

Also you might want to try using pylint to check for common errors or warnings. It would have thrown a warning for the unescaped directory name.

Update:

In fact I would just simplify it to this:

import globfile_list = glob.glob(r"C:\Data\*.csv")print('file_list {}'.format(file_list))