Python os.path is ntpath, how?

那年仲夏 提交于 2019-12-23 08:03:10

问题


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

>>> import os.path
>>> os.path
<module 'ntpath' from 'C:\Python26\lib\ntpath.pyc'>
>>>

回答1:


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).




回答2:


>>> 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


来源:https://stackoverflow.com/questions/2126301/python-os-path-is-ntpath-how

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!