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
Python os.path is ntpath, how?
提问by Alex Martelli
Can someone tell me how Python "aliases" os.pathto ntpath?
有人能告诉我 Python 是如何“别名”os.path的ntpath吗?
>>> 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.path是ntpath在你的平台上(无疑于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

