Professional Documents
Culture Documents
TMP - .Log - in Python - Stack Overflow
TMP - .Log - in Python - Stack Overflow
1 of 2
http://stackoverflow.com/questions/1090651/search-a-folder-for-files-l...
sign up
log in
tour
help
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute:
linux
file
*tmp*.log
folders
Miles
15.9k
JustRegisterMe
3
41
66
752
3 Answers
Use the
glob
module.
44
67
Worked like a charm, thanks for this very fast answer! JustRegisterMe Jul 7 '09 at 6:26
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
19
25
The code below expands on previous answers by showing a more complicated search case.
I had an application that was heavily controlled by a configuration file. In fact there were many
versions of the configuration each with different tradeoffs. So one configuration set would
result in a thorough work but would be very slow while another will be much faster but wont be
as thorough, etc. So the GUI would have a configuration combo box with options
corresponding to different configurations. Since I felt that the set of configurations will be
growing over the time, I did not want to hardcode the list of files and the corresponding options
(and their order) in the application but instead resorted to a file naming convention that would
convey all this information.
The naming convention that I used was as following. Files are located in directory
$MY_APP_HOME/dat. File name begins with my_config_ followed by the combo index
number, followed by the text for the combo item. For example: If the directory contained
(among others) files my_config_11_fast_but_sloppy.txt, my_config_100_balanced.txt,
my_config_3_thorough_but_slow.txt, my combo box would have options (in that order):
Thorough But Slow, Fast But Sloppy, Balanced.
So at runtime I needed to
23-11-2015 02:50 PM
linux - Search a folder for files like "/*tmp*.log" in Python - Stack Ove...
2 of 2
http://stackoverflow.com/questions/1090651/search-a-folder-for-files-l...
23-11-2015 02:50 PM