Sfdfsg
Sfdfsg

Reputation: 127

How to fix pattern, that I use to get list of files in folder with standard library glob?

I have the following files:

/tmp/test_glob/client.log.71.gz
/tmp/test_glob/client.log.63.gz
/tmp/test_glob/client.log.11
/tmp/test_glob/core_dump.log
/tmp/test_glob/client.log.32
/tmp/test_glob/dm.log
/tmp/test_glob/client.log
/tmp/test_glob/client.log.1
/tmp/test_glob/client.log.64.gz

I want to get all .log files, EXCEPT the files, that end with .gz. The desired result should be the following:

/tmp/test_glob/client.log.11
/tmp/test_glob/core_dump.log
/tmp/test_glob/client.log.32
/tmp/test_glob/dm.log
/tmp/test_glob/client.log
/tmp/test_glob/client.log.1

I have written this simple code:

import glob
import os

glob_pattern = u'*.log*'
for log_path in glob.glob(os.path.join('/tmp/test_glob', glob_pattern)):
    print('log_path: ', log_path)

but it returns all file from folder /tmp/test_glob/

I tried to modify this pattern like this:

glob_pattern = u'*.log.[0-9][0-9]'

but it returns only

/tmp/test_glob/client.log.11
/tmp/test_glob/client.log.32

How to fix this pattern ?

Upvotes: 0

Views: 42

Answers (3)

Dan D.
Dan D.

Reputation: 74655

That isn't a glob pattern. You don't want glob. You want to use the re module functions to filter the results of os.listdir.

Upvotes: 0

Domajno
Domajno

Reputation: 637

Try **/*.log!(*.gz)

Test using globster.xyz

Upvotes: 0

Mikael Brenner
Mikael Brenner

Reputation: 357

Using Pythex(a Python regex tester), the match string

glob_pattern = u'.*(\.log)(?!.*(gz)).*'

Worked well for your goal.

Upvotes: 1

Related Questions