You can use os.path.splitext
os.path.splitext(filename)[0]
Of the returned two element array, the first element is just the filename, the second element is the extension. To be extra safe, you could double check that you did indeed grab a .py
file:
if os.path.splitext(filename)[1] == '.py':
# do stuff