Python教程

python os模块和os.path模块

本文主要是介绍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())

这篇关于python os模块和os.path模块的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!