python os模块和os.path模块

一、os

python库中有os.py但是其中并没有定义相关函数,这些函数(例如:os.open , os.makdir)来自于其他文件

以下是os.pyt部分源代码

if 'posix' in _names:
    name = 'posix'
    linesep = '\n'
    from posix import 
    try:
        from posix import _exit
        __all__.append('_exit')
    except ImportError:
        pass
    import posixpath as path

    try:
        from posix import _have_functions
    except ImportError:
        pass

    import posix
    __all__.extend(_get_exports_list(posix))
    del posix

elif 'nt' in _names:
    name = 'nt'
    linesep = '\r\n'
    from nt import
    try:
        from nt import _exit
        __all__.append('_exit')
    except ImportError:
        pass
    import ntpath as path

    import nt
    __all__.extend(_get_exports_list(nt))
    del nt

    try:
        from nt import _have_functions
    except ImportError:
        pass

对于windows系统来说,程序会导入nt.py中的所有方法,因此可以直接使用os."函数名“来进行使用。
以下是nt.py文件中的部分函数

def mkdir(*args, **kwargs): # real signature unknown
    """
    Create a directory.
    
    If dir_fd is not None, it should be a file descriptor open to a directory,
      and path should be relative; path will then be relative to that directory.
    dir_fd may not be implemented on your platform.
      If it is unavailable, using it will raise a NotImplementedError.
    
    The mode argument is ignored on Windows.
    """
    pass

def open(*args, **kwargs): # real signature unknown
    """
    Open a file for low level IO.  Returns a file descriptor (integer).
    
    If dir_fd is not None, it should be a file descriptor open to a directory,
      and path should be relative; path will then be relative to that directory.
    dir_fd may not be implemented on your platform.
      If it is unavailable, using it will raise a NotImplementedError.
    """
    pass

二、os.path

import ntpath as path

os.py将ntpath模块导入并且重新命名为path,因此可以通过os.path来调用ntpath中的函数。(例如:os.path.exists,os.path.join())

上一篇:C++使用条件运算符进行成绩分级


下一篇:Python 3基础教程17-提问频率较高的几个Python问题