I am trying to get list all the log files (.log) in directory and included all subdirectories.
Thanks in advance......
I am trying to get list all the log files (.log) in directory and included all subdirectories.
Thanks in advance......
Checkout Python Recursive Directory Walker. In short os.listdir() and os.walk() are your friends.
If You want to list in current directory, You can use something like:
import os
for e in os.walk(os.getcwd()):
print e
Just change the
os.getcwd()
to other path to get results there.
import os
import os.path
for dirpath, dirnames, filenames in os.walk("."):
for filename in [f for f in filenames if f.endswith(".log")]:
print os.path.join(dirpath, filename)
You can also use the glob module along with os.walk.
import os
from glob import glob
files = []
start_dir = os.getcwd()
pattern = "*.log"
for dir,_,_ in os.walk(start_dir):
files.extend(glob(os.path.join(dir,pattern)))