• stackoverflow os.path.abspath and os.path.realpath


    os.path.realpath derefences symbolic links on those operating systems which support them.

    os.path.abspath simply removes things like . and .. from the path giving a full path from the root of the directory tree to the named file (or symlink)

    For example, on Ubuntu

    $ ls -l
    total 0
    -rw-rw-r-- 1 guest guest 0 Jun 16 08:36 a
    lrwxrwxrwx 1 guest guest 1 Jun 16 08:36 b -> a
    
    $ python
    Python 2.7.11 (default, Dec 15 2015, 16:46:19) 
    [GCC 4.8.4] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    
    >>> from os.path import abspath, realpath
    
    >>> abspath('b')
    '/home/guest/play/paths/b'
    
    >>> realpath('b')
    '/home/guest/play/paths/a'

    Symlinks can contain relative paths, hence the need to use both. The inner call to realpath might return a path with embedded .. parts, which abspath then removes.

    For your stated scenario, there is no reason to combine realpath and abspath, since os.path.realpath actually calls os.path.abspath before returning a result (I checked Python 2.5 to Python 3.6).

    • os.path.abspath returns the absolute path, but does NOT resolve symlinks.
    • os.path.realpath will first resolve any symbolic links in the path, and then return the absolute path.

    However, if you expect your path to contain a ~neither abspath or realpath will resolve ~ to the user's home directory, and the resulting path will be invalid. You will need to use os.path.expanduser to resolve this to the user's directory.

    For the sake of a thorough explanation, here are some results which I've verified in Windows and Linux, in Python 3.4 and Python 2.6. The current directory (./) is my home directory, which looks like this:

    myhome
    |- data (symlink to /mnt/data)
    |- subdir (extra directory, for verbose explanation)
    # os.path.abspath returns the absolute path, but does NOT resolve symlinks
    os.path.abspath('./')
    '/home/myhome'
    os.path.abspath('./subdir/../data')
    '/home/myhome/data'
    
    
    # os.path.realpath will resolve symlinks AND return an absolute path from a relative path
    os.path.realpath('./')
    '/home/myhome'
    os.path.realpath('./subdir/../')
    '/home/myhome'
    os.path.realpath('./subdir/../data')
    '/mnt/data'
    
    # NEITHER abspath or realpath will resolve or remove ~.
    os.path.abspath('~/data')
    '/home/myhome/~/data'
    
    os.path.realpath('~/data')
    '/home/myhome/~/data'
    
    # And the returned path will be invalid
    os.path.exists(os.path.abspath('~/data'))
    False
    os.path.exists(os.path.realpath('~/data'))
    False
    
    # Use realpath + expanduser to resolve ~
    os.path.realpath(os.path.expanduser('./subdir/../data'))
    '/mnt/data'
  • 相关阅读:
    Oracle逻辑备份与恢复
    Java调用webservice接口方法
    Weblogic11g下调WebService出现的一系列问题
    数字转中文【适用于金额转换和普通数字转换】
    Tomcat6环境JBPM4.4报错:java.lang.ClassNotFoundException: de.odysseus.el.util.SimpleResolver
    Hibernate之Criteria的完整用法
    Oracle SQL Developer连接报错(ORA-12505)
    Windows Storage Server 2008 R2 Standard(64位)之ASM(Automated Storage Manager)管理
    REHL5.5 linux的postfix的邮件服务器配置 (笔记)
    JVM中class文件探索与解析
  • 原文地址:https://www.cnblogs.com/YingxuanZHANG/p/8819121.html
Copyright © 2020-2023  润新知