The line
if lookfor in files:
says that the following code should be executed if the list files contains the string given in lookfor.
However, you want that the test should be that the found file name starts with the given string and continues with a ..
Besides, you want the real filename to be determined.
So your code should be
import os
from os.path import join, splitext
lookfor = "sh"
found = None
for root, dirs, files in os.walk('C:\\'):
for file in files: # test them one after the other
if splitext(filename)[0] == lookfor:
found = join(root, file)
print "found: %s" % found
break
if found: break
This can even be improved, as I don't like the way how I break the outer for loop.
Maybe you want to have it as a function:
def look(lookfor, where):
import os
from os.path import join, splitext
for root, dirs, files in os.walk(where):
for file in files: # test them one after the other
if splitext(filename)[0] == lookfor:
found = join(root, file)
return found
found = look("sh", "C:\\")
if found is not None:
print "found: %s" % found