开发者

Python os.path is ntpath, how?

Can someone tell me how Python "aliases" os.path to ntpath?

>>> import os.p开发者_开发技巧ath
>>> os.path
<module 'ntpath' from 'C:\Python26\lib\ntpath.pyc'>
>>>


Look at os.py, lines 55-67:

elif 'nt' in _names:
    name = 'nt'
    linesep = '\r\n'
    from nt import *
    try:
        from nt import _exit
    except ImportError:
        pass
    import ntpath as path

    import nt
    __all__.extend(_get_exports_list(nt))
    del nt

The import ntpath as path is the specific statement that causes os.path to be ntpath on your platforms (doubtlessly Windows).


>>> import os as my_aliased_module
>>> my_aliased_module
<module 'os' from 'C:\Program Files\Python 2.6\lib\os.pyc'>

EDIT: And since import is a simple statement in Python, you can do neat stuff like:

import sys

if sys.platform == 'win32':
  import windows_module as my_module
else:
  import unix_module as my_module
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜