2010-01-24 4 views
6

Kann mir jemand sagen, wie Python "Aliase" os.path bis ntpath?Python os.path ist ntpath, wie?

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

Eines der wirklich großen Dinge an der Python Standard Library ist, dass Sie sich die Quelle ansehen können. Ich empfehle wirklich, dort herumzustochern, um zu sehen, wie das Zeug gemacht wird. –

Antwort

10

Betrachten Sie os.py, Zeilen 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 

Die import ntpath as path ist die Spezifikation ic-Anweisung, die os.path zu ntpath auf Ihren Plattformen (zweifellos Windows) verursacht.

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

EDIT: Da import eine einfache Erklärung in Python ist, können Sie nette Dinge tun:

import sys 

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