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.
views:
531answers:
2
+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
2009-07-07 06:18:58
Worked like a charm, thanks for this very fast answer!
JustRegisterMe
2009-07-07 06:26:01
+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
2009-07-07 18:43:59
Good idea, this will be better for more complicated searches which need more than *.
JustRegisterMe
2009-07-13 07:03:56