S. Alperen
S. Alperen

Reputation: 33

Python Glob search for specific filenames at the middle

I have a folder with path

/home/alperen/Desktop/test

and with files:

000001asdd.png
000005_C.png
000010-asda.png
000002gfg.png 000006fkjfkj.png
.. etc

Here is my code portion to check specific files in this directory

for name in glob.glob("/home/alperen/Desktop/test/*001*.*"):
    print (name)

It gives correct output:

/home/alperen/Desktop/test/000012-asda.png
/home/alperen/Desktop/test/000001asdd.png
/home/alperen/Desktop/test/000010-asda.png
/home/alperen/Desktop/test/A000011-adsa.png

However when I try to use the following it does not work:

print(read_dir)        
for name in glob.glob(read_dir + "/*001*.*"):
    print (name)

It only prints read_dir itself and nothing else.

home/alperen/Desktop/test

Can anybody help me what is happening? Thanks.

Upvotes: 0

Views: 707

Answers (1)

DirtyBit
DirtyBit

Reputation: 16772

Try:

for name in glob.glob("/" + read_dir + "/*001*.*"):
    print(name)

Why:

Theread_dir is missing a leading / as it can be seen from the output:

home/alperen/Desktop/test

Where in the first case when you gave a complete path, it had a leading slash /, as it can be seen here:

for name in glob.glob("/home/alperen/Desktop/test/*001*.*"):

Upvotes: 1

Related Questions