Python os.path 是ntpath,如何?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2126301/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 23:51:39  来源:igfitidea点击:

Python os.path is ntpath, how?

pythonpathmodulealias

提问by Alex Martelli

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

有人能告诉我 Python 是如何“别名”os.pathntpath吗?

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

回答by Alex Martelli

Look at os.py, lines 55-67:

查看os.py,第 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 pathis the specific statement that causes os.pathto be ntpathon your platforms (doubtlessly Windows).

import ntpath as path是使特定的语句os.pathntpath在你的平台上(无疑于Windows)。

回答by Max Shawabkeh

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

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

编辑:由于import是 Python 中的一个简单语句,您可以执行以下操作:

import sys

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