Hey guys,

When using the glob import, I want it to scan all of the files in a directory, but not scan other directories in that directory. For example, say I am in a directory with the following files:

file, file1, file2, DIRECTORY1, DIRECTORY2

When I use the glob command:

'''This is needed for the 'glob' import to understand where it is looking for the raw files'''

file_list = glob.glob("*")

It returns the files, and the directories. Without adding some special extension to my files to avoid this situation, is there a way to tell the glob.glob command to avoid directories?

9 Years
Discussion Span
Last Post by vegaseat

When you iterate over your file_list you could ignore the directories yourself like so:

file_list = glob.glob('*')
for file in file_list:
    if not os.path.isdir(file):
        # Do my stuff now on only files

You could also just go through and strip out the directories with a list comprehension:

file_list = [ file for file in glob.glob('*') if not os.path.isdir(file) ]

The main idea about using module glob is to look for specific files. If you give glob a more specific wildcard like '*.jp*' then it will only list .jpg or .jpeg files and not subdirectories.

The way you are using glob makes little sense and you might better use os.listdir(folder).

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.