That is because your module in not in a standard location that python can find it in. It is not that inspect.getsourcefile() is not working.
Actually if you try reloading your filegen module after the os.chdir(), you will see that import fails. If you copy your filegen directory to the site-packages directory of python, you will get the complete path of the module no matter where you are inside your script. -- http://mail.python.org/mailman/listinfo/python-list