微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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())

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐