OS.path模塊的詳細使用說明

大家好,從本週起早起Python將持續更新由小甜同學從初學者的角度學習Python的筆記,其特點就是全文大多由新手易理解的代碼與註釋及動態演示剛入門的讀者千萬不要錯過!

爲了配合Python辦公自動化系列文章,本文帶來的是偷學Python第二十六天:OS.path模塊的詳細使用說明,其他內容將在近期更新完畢,本文目錄如下

相對路徑與絕對路徑

所謂絕對路徑就是一個完整的路徑,例如C:\windows\system32\cmd.exe

相對路徑就是從當前路徑開始的路徑。使用一個.來表示當前目錄,兩個點..表示當前的父目錄。例如當前目錄爲C:\windows要描述上述的路徑只需要.\system32\cmd.exe或者system32\cmd.exe,前者更爲嚴格後者較爲簡單;如果當前路徑爲C:\windows\Wed要找到上述路徑就可以../system32\cmd.exe

相對路徑在編程中更爲常用,因爲程序媛永遠不會知道用戶將程序放在哪個盤裏面,所有用相對路徑就完美的結局了這個問題!

OS.path模塊

Python中的os.path模塊主要用於獲取文件的屬性。

path.abspath()

path.abspath()方法返回絕對路徑,參數爲一個路徑,示例代碼如下

import os
file = os.path.abspath(__file__)  # __file__ 表示當前文件
print(file) 

path.basename()

os.path.basename(path) 返回文件名,示例代碼如下

import os
file = os.path.basename(__file__)
print(file)  # 02path.basename()方法.py

path.dirname()

path.dirname()方法返回返回path的文件夾,示例代碼如下

import os
file = os.path.dirname(__file__)
print(file)  # (此處...)01 基礎部分/23os.path模塊

path.exists()

path.exists()方法把當前路徑包含的~~user修改爲家目錄,示例代碼如下

import os
file = os.path.expanduser("~\\甜甜\\1.txt")  # 一個反反斜線是轉義字符
print(file)  # C:\Users\admin\甜甜\1.txt

path.expanduser()

path.expanduser()方法把當前路徑包含的~或者~user修改爲家目錄,示例代碼如下

import os
file = os.path.expanduser("~\\甜甜\\1.txt")  # 一個反反斜線是轉義字符
print(file)  # C:\Users\admin\甜甜\1.txt

path.expandvars()

path.expandvars()方法對路徑中出現的$name 或者 ${name}進行系統環境變量路徑的取代.,示例代碼如下

import os
file = os.path.expandvars("$PATH")
print(file)  # D:\Program...npm

獲取文件的訪問/修改/創建時間

  • os.path.getatime(path) 返回最近訪問時間(浮點型秒數)

  • os.path.getmtime(path)返回最近文件修改時間

  • os.path.getctime(path)返回文件路徑創建的時間

import os
import time
access_time = os.path.getatime(__file__)  # 最近訪問時間
modify_time = os.path.getmtime(__file__)  # 最近修改時間
create_time = os.path.getctime("02path.basename()方法.py")  # 返回文件的創建時間
print(access_time)  # 1590401407.8338118
print(modify_time)  # 1590401407.7799587
print(create_time)  # 1590398628.070578
# 利用time模塊的ctime方法轉變爲正常時間
print(time.ctime(access_time))  # Mon May 25 18:14:49 2020

path.getsize()

path.getsize()方法返回返回文件的大小,如果文件不存在拋出異常,示例代碼如下

import os
file = os.path.getsize(__file__)  # 返回字節數
print(file)  # 125

判斷路徑的屬性

os.path.isabs/.isfile/.isdir/.islink/.ismount(path)判斷路徑是否爲絕對路徑/文件/文件夾/鏈接/掛載點

import os
file1 = os.path.isabs("09判斷路徑的屬性.py")  # False
file2 = os.path.isfile("09判斷路徑的屬性.py")  # True
file3 = os.path.isdir("..\\")  # True
file4 = os.path.islink(__file__)  # False
file5 = os.path.ismount("D:")  # True
print(file1, file2, file3, file4, file5)

path.join()

os.path.join(path1[, path2[, …]])把目錄和文件名合成一個路徑

import os
file_name = os.path.basename(__file__)  # 獲取文件名
file = os.path.join("D:\\", file_name)  # 拼合路徑
print(file)  # D:\10path.join方法.py

path.normcase()

os.path.normcase(path) 在不區分大小寫的文件系統上, 它把路徑轉換爲小寫字母。在Windows上, 它把正斜槓轉換爲反斜槓

import os
file = os.path.normcase("D:\\file/1.txt")  # 將一個路徑轉變爲適合自己系統的路徑
print(file)  # d:\file\1.txt

path.normpath()

os.path.normpath(path) 規範path的字符串形式

import os
file = os.path.normpath("01 基礎部分/23os.path模塊/12path.normpath()方法.py")
print(file)  # 01 基礎部分\23os.path模塊\12path.normpath()方法.py

path.realpath()

os.path.realpath(path) 返回path的真實路徑

import os
file = os.path.realpath(__file__)
print(file)  # (省略。。。))01 基礎部分\23os.path模塊\13path.realpath()方法.py

path.relpath()

os.path.relpath(path[, start]) 從start開始計算相對路徑

import os
file = os.path.relpath(__file__, "01 基礎部分")
print(file)  # ..\14path.relpath()方法.py

分割路徑

  • os.path.split(path) 把路徑分割成dirname和basename,返回一個元組

  • os.path.splitdrive(path) 一般用在windows下,返回驅動器名和路徑組成的元組

  • os.path.splitext(path) 分割路徑,返回路徑名和文件擴展名的元組

import os
# 返回文件夾與文件的元組
print(os.path.split(__file__))  # ('Y:/.../01 基礎部分/23os.path模塊', '15分割路徑.py')
# 返回驅動器名和路徑組成的元組
print(os.path.splitdrive(__file__))  # ('Y:', '.../01 基礎部分/23os.path模塊/15分割路徑.py')
# 返回路徑名和擴展的元組
print(os.path.splitext(__file__))  # ('Y:.../01 基礎部分/23os.path模塊/15分割路徑', '.py')
往期文章讀完本文你就瞭解什麼是文本分析

綜述:文本分析在市場營銷研究中的應用
從記者的Twitter關注看他們稿件的黨派傾向?

Pandas時間序列數據操作
readability: 英文文本數據可讀性庫

Matplotlib可視化教程~

Matplotlib中的plt和ax都是啥?

70G上市公司定期報告數據集
5個小問題帶你理解列表推導式
文本數據清洗之正則表達式
Python網絡爬蟲與文本數據分析
如何批量下載上海證券交易所上市公司年報
Numpy和Pandas性能改善的方法和技巧
漂亮~pandas可以無縫銜接Bokeh
YelpDaset: 酒店管理類數據集10+G

覺得這篇文章還不錯?那就點亮「在看」讓更多人看到!

- THE END-

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章