Archived:How to search a file extension using PySymbian
Article Metadata
The code snippet below in Category:PySymbian shows how to search a file extension.
import os, appuifw
def findfile(folder, file_extension):
p=[]
stack = [(folder, os.listdir(folder))]
while stack:
folder, names = stack[-1]
while names:
name = names.pop()
path = os.path.join(folder, name)
if os.path.isfile(path):
if name.lower().endswith(file_extension):
p.append(path)
elif os.path.isdir(path):
stack.append((path, os.listdir(path)))
break
else:
stack.pop()
return p
# Ask for the extension of the file to search for
file_extension = appuifw.query(u'Enter file extension', 'text')
# Specify the folder to look in; file extension is used in lower case for
# case insensitive comparison
path = findfile('C:\\', file_extension.lower())
# Display the path or error message if not found
if path is None:
appuifw.note(u'Not found')
else:
for i in path:
print i

