简体   繁体   中英

Python os.path is ntpath, how?

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

>>> import os.path
>>> 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM