ankit agrawal
ankit agrawal

Reputation: 311

Glob module returning wrong value

I have bunch of folders like below example and inside each folders have following number of files. Now when I want to know how many files are inside Pos2 folder then I am getting wrong output. For other folders it is giving correct. Am I doing anything wrong?

./a/Pos2/              8497 files              
./a/Pos22/             4227 files 
./a/Pos23/              2052 files 
./a/Pos26/              2633 files 

import glob
DIR='a/Pos2'
files = [f for f in glob.glob(DIR + "**/*.mat")]
len(files)

I am getting the answer 17409. It is adding the files from all folders. I don't why. Any clue what is happening?

Upvotes: 0

Views: 94

Answers (1)

mkrieger1
mkrieger1

Reputation: 23140

DIR='a/Pos2'
files = [f for f in glob.glob(DIR + "**/*.mat")]

DIR + "**/*.mat" results in "a/Pos2**/*.mat", which matches all of ./a/Pos2/*.mat, ./a/Pos22/*.mat, ./a/Pos23/*.mat, ./a/Pos26/*.mat.

To only find the files in a/Pos2, use "a/Pos2/*.mat", or DIR + "/*.mat".

Or, to find all files in all subfolders of a/Pos2, use "a/Pos2/**/*.mat", or DIR + "/**/*.mat". Note the additional /.

Upvotes: 1

Related Questions