views:

531

answers:

2

As the title says, I'm using Linux, and the folder could contain more than one file, I want to get the one its name contain *tmp*.log (* means anything of course!). Just like what I do using Linux command line.

+9  A: 

Use the glob module.

>>> import glob
>>> glob.glob('./[0-9].*')
['./1.gif', './2.txt']
>>> glob.glob('*.gif')
['1.gif', 'card.gif']
>>> glob.glob('?.gif')
['1.gif']
Otto Allmendinger
Worked like a charm, thanks for this very fast answer!
JustRegisterMe
+1  A: 

The glob answer is easier, but for the sake of completeness: You could also use os.listdir and a regular expression check:

import os
import re
dirEntries = os.listdir(path/to/dir)
for entry in dirEntries:
  if re.match(".*tmp.*\.log", entry):
    print entry
PTBNL
don't even have to use re.
ghostdog74
Good idea, this will be better for more complicated searches which need more than *.
JustRegisterMe