python中os.path.abspath和os.path.realpath區別


 Python在使用os包的過程中,經常遇到返回wen文件路徑的方法,那os.path.abspath()和os.path.realpath()的區別是什么呢,有時候傻傻分不清楚,現在我們就來用實例來說明下,他兩個的區別:

1.os.path.abspath()

  1.  
    返回一個目錄的絕對路徑
  2.  
    Return an absolute path.
  1.  
    >>> os.path.abspath("/etc/sysconfig/selinux")
  2.  
    '/etc/sysconfig/selinux'
  3.  
    >>> os.getcwd()
  4.  
    '/root'
  5.  
    >>> os.path.abspath("python_modu")
  6.  
    '/root/python_modu'

2.os.path.realpath()

  1.  
    返回指定文件的標准路徑,而非軟鏈接所在的路徑
  2.  
    Return the canonical path of the specified filename, eliminating any
  3.  
    symbolic links encountered in the path.
  1.  
    >>> os.path.realpath("/etc/sysconfig/selinux")
  2.  
    '/etc/selinux/config'
  3.  
    >>> os.path.realpath("/usr/bin/python")
  4.  
    '/usr/bin/python2.7'

 

現在是不是一目了然,兄弟還不趕緊關注。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM