[Python基礎]010.os模組(2)

來源:互聯網
上載者:User

標籤:

os模組(2)

  • 介紹
  • os
    • 常量
    • 路徑
    • 判斷路徑屬性
    • 路徑變換
    • 檔案屬性
    • 相同檔案
介紹

- os.path模組,主要處理路徑操作,包含了各種處理檔案和檔案名稱的方法。

os.path常量
  • os.path.sep 路徑分隔字元 (Unix為 /,Win為 \\)
  • os.path.pathsep 多個路徑間的分隔字元,多用於環境變數 (Unix為 :, Win為 ;)
  • os.path.extsep 尾碼名符號 一般為 .
路徑
  • os.path.split 分割路徑為目錄名和檔案名稱
  • os.path.dirname 目錄名
  • os.path.basename 檔案名稱
  • os.path.splitext 分割路徑為檔案和副檔名
  • os.path.join 路徑組合

代碼

import osfilename = ‘/Users/superdo/test.txt‘# 分割路徑_dir, _file = os.path.split(filename)print _dir      # /Users/superdoprint _file     # test.txt# 目錄名print os.path.dirname(filename)     # /Users/superdo# 檔案名稱print os.path.basename(filename)    # test.txt# 副檔名_filename, _ext = os.path.splitext(filename)print _filename     # /Users/superdo/testprint _ext          # .txt# 路徑組合f = os.path.join(‘Users‘, ‘superdo‘, ‘test.txt‘)print f     # Users/superdo/test.txt

 

判斷路徑屬性
  • os.path.exists 檔案是否存在
  • os.path.isdir 是否是目錄
  • os.path.isfile 是否是檔案
  • os.path.islink 是否是串連檔案
  • os.path.ismount 是否是掛載檔案
  • os.path.isabs 是否是絕對路徑

代碼

import osfilename = ‘/Users/superdo/test.txt‘print os.path.exists(filename)      # 判斷存在print os.path.isdir(filename)       # 判斷檔案夾print os.path.isfile(filename)      # 判斷檔案print os.path.islink(filename)      # 判斷串連檔案print os.path.ismount(filename)     # 判斷掛載檔案print os.path.isabs(filename)       # 判斷絕對路徑

 

路徑變換
  • os.path.relpath 相對路徑
  • os.path.abspath 絕對路徑
  • os.path.normpath 標準化路徑
  • os.path.commonprefix 獲得共同路徑

代碼

import osfilename = ‘/Users/superdo/ac/../path.txt‘# 獲得相對路徑print os.path.relpath(filename)     # 獲得相對於當前路徑的路徑print os.path.relpath(filename, ‘/Users‘)   # 獲得相對於/Users的路徑# 絕對路徑print os.path.abspath(filename)# 標準化路徑print os.path.normpath(filename)# 獲得多個路徑中的共同路徑f1 = ‘/Users/superdo/zergling/file.txt‘f2 = ‘/Users/superdo/ac/file.txt‘f3 = ‘/Users/superdo/horse/file.txt‘print os.path.commonprefix([f1, f2, f3])    # /Users/superdo/

 

檔案屬性
  • os.path.getatime 訪問時間 (從os.stat獲得)
  • os.path.getmtime 修改時間(從os.stat獲得)
  • os.path.getctime 建立時間(從os.stat獲得)
  • os.path.getsize 檔案大小(從os.stat獲得)

代碼

import osimport filename = ‘/Users/superdo/test.txt‘print os.path.getatime(filename)print os.path.getmtime(filename)print os.path.getctime(filename)print os.path.getsize(filename)

 

相同檔案
  • os.path.samefile 對比檔案
  • os.path.sameopenfile 對比開啟檔案
  • os.path.samestat 對比檔案stat

代碼

import osf1 = ‘/Users/superdo/file.txt‘f2 = ‘/Users/superdo/file.txt‘print os.path.samefile(f1, f2)fp1 = open(f1)fp2 = open(f2)print os.path.sameopenfile(fp1.fileno(), fp2.fileno())fs1 = os.stat(f1)fs2 = os.stat(f2)print os.path.samestat(fs1, fs2)

 

 

 

本站文章為 寶寶巴士 SD.Team 原創,轉載務必在明顯處註明:(作者官方網站: 寶寶巴士 ) 
轉載自【寶寶巴士SuperDo團隊】 原文連結: http://www.cnblogs.com/superdo/p/4719191.html

 

 

 

[Python基礎]010.os模組(2)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.