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?

Recommended Answers

All 2 Replies

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).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.