_fileNameToModuleName drek removal

Phlip phlip_cpp at yahoo.com
Sun Jun 2 11:20:06 CEST 2002


Phlip wrote:

> Ogle this:
> 
> def _fileNameToModuleName(fileName):
>     'TODO  is there a way in Python to avoid this drek??'
> 
>     assert fileName[-3:] == '.py'
>     if fileName[0] != '.':  os.path.join('.', fileName)
>     fileName = fileName[:-3]
>     moduleName = string.split(fileName, os.sep)[1:]
>     moduleName = string.join(moduleName, '.')
>     assert moduleName[0] != '.'
>     return moduleName
> 
> That, platform neutral, converts a file name ./yak/yak/yak.py 
> into a module name yak.yak.yak 
> 
> Is there some cleaner way to do this? Preferably built-in?

Python's interpreter only goes the other way, from yak.yak.yak 
to yak/yak/yak.py. The language had no reason to do this until I
came along. So I'l have to guess there is no cleaner way than 
the above string and path manipulation.

-- 
  Phlip
                http://www.greencheese.org/YaAw
  --  Got in trouble at StarBucks. I tried to order
        "A double latte mocha and a body piercing."  --



More information about the Python-list mailing list