
作者:小伍哥
來源:AI入門學(xué)習(xí)
os,語義操作系統(tǒng),所以該模塊就是操作系統(tǒng)相關(guān)的功能了,用于處理文件和目錄這些我們?nèi)粘J謩有枰龅牟僮?,比?strong>新建文件夾、獲取文件列表、刪除某個文件、獲取文件大小、重命名文件、獲取文件修改時間等,該模塊就包含了大量的操作系統(tǒng)操作函數(shù),精選常用的進(jìn)行解析,希望對大家有所幫助。
os下面有很多函數(shù),調(diào)用方式一般為os.name,其中比較特殊的是Path,Path模塊是OS的一個子模塊,下面又有很多的函數(shù),調(diào)用方式一般為os.path.isfile。剛接觸的可能不大明白,需要注意下。
#加載
import os
#查看os下的函數(shù)
print(dir(os))
#查看os.path下的函數(shù)
print(dir(os.path))
01、os.name()
描述:顯示當(dāng)前使用的平臺,'nt'表示W(wǎng)indows,'posix' 表示Linux
語法:os.name
os.name
'nt'
02、os.getcwd()
描述:返回當(dāng)前進(jìn)程的工作目錄。
語法:os.getcwd()
os.getcwd()
'C:\\Users\\wuzhengxiang'
03、os.chdir()
描述:改變當(dāng)前工作目錄到指定的路徑。
語法:os.chdir(path)
#查看當(dāng)前目錄os.getcwd()
'C:\\Users\\wuzhengxiang'#重新設(shè)置當(dāng)前工作空間
os.chdir('C:/Users/wuzhengxiang/Desktop/股票數(shù)據(jù)分析')
#再次查看當(dāng)前目錄,已經(jīng)變成新的了
os.getcwd()
'C:\\Users\\wuzhengxiang\\Desktop\\股票數(shù)據(jù)分析'
04、os.makedirs()
描述:方法用于遞歸創(chuàng)建目錄。像 mkdir(), 但創(chuàng)建的所有intermediate-level文件夾需要包含子目錄。
語法:os.makedirs(path, mode=0o777)
os.makedirs('C:/Users/wuzhengxiang/Desktop/股票數(shù)據(jù)分析/1122', mode=0o777)
05、os.mkdir()
描述:以數(shù)字權(quán)限模式創(chuàng)建目錄。默認(rèn)的模式為 0777 (八進(jìn)制)。
語法:os.mkdir(path[, mode])
#創(chuàng)建新的目2233
os.mkdir('C:/Users/wuzhengxiang/Desktop/股票數(shù)據(jù)分析/2233', mode=0777 )
06、os.listdir()
描述:列出目錄下的所有文件和文件夾
語法:os.listdir(path)
os.listdir('C:/Users/wuzhengxiang/Desktop/股票數(shù)據(jù)分析')
['ETF研究.py', 'foo.txt', 'pi.txt', 'render.html']
os.listdir('.')
['ETF研究.py', 'foo.txt', 'pi.txt', 'render.html']
07、os.remove()
描述:用于刪除指定路徑的文件。如果指定的路徑是一個目錄,將拋出OSError。
語法:os.remove(path)
os.remove('C:/Users/zhengxiang.wzx/Desktop/timg.jpg')
08、os.rename()
描述:命名文件或目錄,能對相應(yīng)的文件進(jìn)行重命名
語法:os.rename(src, dst)
參數(shù)
#空間設(shè)置
data_path = 'C:/Users/zhengxiang.wzx/Desktop/微博情緒識別'
os.chdir(data_path)#設(shè)置工作空間
os.getcwd()
'C:\\Users\\zhengxiang.wzx\\Desktop\\微博情緒識別'
os.rename("圖片下載.py","圖片下載1.py")
09、os.renames()
描述:用于遞歸重命名目錄或文件。類似rename()。既可以重命名文件, 也可以重命名文件的上級目錄名
語法:os.renames(old, new)
參數(shù):
os.chdir('C:/Users/wuzhengxiang/Desktop/Python知識點總結(jié)')
os.getcwd()
#文件夾和文件同時命名
os.renames("test/Python 63個內(nèi)置函數(shù)詳解.py","test2/內(nèi)置函數(shù)詳解.py")
os.listdir()
['kaggle',
'test2',
'股票分析',
'課程資源'
10、os.linesep()
描述:當(dāng)前平臺用于分隔(或終止)行的字符串。它可以是單個字符,如 POSIX 上是 '\n',也可以是多個字符,如 Windows 上是 '\r\n'。在寫入以文本模式(默認(rèn)模式)打開的文件時,請不要使用 os.linesep 作為行終止符,請在所有平臺上都使用一個 '\n' 代替。
語法:os.linesep
os.linesep
'\r\n'
11、os.pathsep()
描述:操作系統(tǒng)通常用于分隔搜索路徑(如 PATH)中不同部分的字符,如 POSIX 上是 ':',Windows 上是 ';'。在 os.path 中也可用。
語法:os.pathsep
os.pathsep
';'
12、os.close()
描述:關(guān)閉指定的文件描述符 fd
語法:os.close(fd)
fd = os.open( "foo.txt", os.O_RDWR|os.O_CREAT )
os.write(fd, bytes("This is test", encoding = "utf8"))
os.close( fd )
13、os.stat()
描述:獲取文件或者目錄信息
語法:os.stat(path)
os.stat('C:/Users/wuzhengxiang/Desktop/股票數(shù)據(jù)分析\\pi.txt')
os.stat_result(st_mode=33206, st_ino=22236523160361562, st_dev=2419217970, st_nlink=1
, st_uid=0, st_gid=0, st_size=53, st_atime=1589638199, st_mtime=1589638199, st_ctime=1581868007)
14、os.sep()
描述:顯示當(dāng)前平臺下路徑分隔符,在 POSIX 上是 '/',在 Windows 上是是 '\\'
語法:os.sep
os.sep'
\\'
15、os.path.abspath()
描述:返回文件的絕對路徑
語法:os.path.abspath(path)
#Excel文件
os.path.abspath('all_data.xlsx')
'C:\\Users\\zhengxiang.wzx\\all_data.xlsx'
#圖片文件
os.path.abspath('IMG_7358.JPG')
'C:\\Users\\zhengxiang.wzx\\IMG_7358.JPG'
16、os.path.basename()
描述:返回文件名,純粹字符串處理邏輯,路徑錯誤也可以
語法:os.path.basename(path)
os.path.basename('C:\\Users\\zhengxiang.wzx\\all_data.xlsx')
'all_data.xlsx'
17、os.path.commonprefix()
描述:返回list(多個路徑)中,所有path共有的最長的路徑
語法:os.path.commonprefix(list)
os.path.commonprefix(['http://c.biancheng.net/python/aaa', 'http://c.biancheng.net/shell/'])'http://c.biancheng.net/'os.path.commonprefix(['http://bianc/python/aaa', 'http://c.biancheng.net/shell/'])'http://'
18、os.path.dirname()
描述:返回文件路徑
語法:os.path.dirname(path)
os.path.dirname('C://my_file.txt')
'C://'
os.path.dirname('C://python//my_file.txt')
'C://python'
19、os.path.exists()
描述:如果路徑 path 存在,返回 True;如果路徑 path 不存在,返回 False。
語法:os.path.exists(path)
os.path.exists('C:/Users/wuzhengxiang/Desktop/股票數(shù)據(jù)分析/pi.txt')
True
os.path.exists('C:/Users/wuzhengxiang/Desktop/股票數(shù)據(jù)分析/')
True
os.path.exists('C:/Users/wuzhengxiang/Desktop/股票數(shù)據(jù)分析/pi_01.txt')
Fals
20、os.path.lexists()
描述:路徑存在則返回True,路徑損壞也返回True, 不存在,返回 False。
語法:os.path.lexists
os.path.lexists('C:/Users/wuzhengxiang/Desktop/股票數(shù)據(jù)分析/pi.txt')
True
os.path.lexists('C:/Users/wuzhengxiang/Desktop/股票數(shù)據(jù)分析/pi_01.txt')
False
21、os.path.expanduser()
描述:把path中包含的"~"和"~user"轉(zhuǎn)換成用戶目錄
語法:os.path.expanduser(path)
os.path.expanduser('~/wuzhengxiang/Desktop/股票數(shù)據(jù)分析/')
'C:\\Users\\wuzhengxiang/wuzhengxiang/Desktop/股票數(shù)據(jù)分析/'
22、os.path.expandvars()
描述:根據(jù)環(huán)境變量的值替換path中包含的"$name"和"${name}"
語法:os.path.expandvars(path)
os.environ['KITTIPATH'] = 'D:/thunder'
path = '$KITTIPATH/train/2011_09_26_drive_0001_sync/proj_depth/velodyne_raw/image_02/0000000013.png'
os.path.expandvars(path)
'D:/thunder/train/2011_09_26_drive_0001_sync/proj_depth/velodyne_raw/image_02/0000000013.png'
23、os.path.getatime()
描述:返回最近訪問時間(浮點型秒數(shù)),從新紀(jì)元到訪問時的秒數(shù)。
語法:os.path.getatime(path)
os.path.getatime('C:/Users/wuzhengxiang/Desktop/股票數(shù)據(jù)分析/pi.txt')
1589638199.1343248
24、os.path.getmtime()
描述:返回最近文件修改時間,從新紀(jì)元到訪問時的秒數(shù)。
語法:os.path.getmtime(path)
os.path.getmtime('C:/Users/wuzhengxiang/Desktop/股票數(shù)據(jù)分析/pi.txt')
1583069050.8148942
25、os.path.getctime()
描述:返回文件 path 創(chuàng)建時間,從新紀(jì)元到訪問時的秒數(shù)。
語法:os.path.getctime(path)
os.path.getctime('C:/Users/wuzhengxiang/Desktop/股票數(shù)據(jù)分析/pi.txt')
1581868007.6123319
26、os.path.getsize()
描述:返回文件大小,如果文件不存在就返回錯誤
語法:os.path.getsize(path)
os.path.getsize('C:/Users/wuzhengxiang/Desktop/股票數(shù)據(jù)分析/test.gif')
1128677
27、os.path.isabs()
描述:判斷是否為絕對路徑,也就是說在WIndow系統(tǒng)下,如果輸入的字符串以" / "開頭,os.path.isabs()就會返回True
語法:os.path.isabs(path)
os.path.isabs('D:/thunder')
True
os.path.isabs('D:\thunder')
False
os.path.isabs('D:\\thunder')
True
28、os.path.isfile()
描述:判斷路徑是否為文件
語法:os.path.isfile(path)
#文件不存在 返回False
os.path.isfile("C:/Users/wuzhengxiang/Desktop/股票數(shù)據(jù)分析/pi_01.txt")
False
os.path.isfile("C:/Users/wuzhengxiang/Desktop/股票數(shù)據(jù)分析/pi.txt")
True#不是文件 返回Falseo
s.path.isfile("C:/Users/wuzhengxiang/Desktop/股票數(shù)據(jù)分析/")
False
29、os.path.isdir()
描述:判斷路徑是否為目錄
語法:os.path.isdir(path)
os.path.isdir('C:/Users/wuzhengxiang/Desktop/股票數(shù)據(jù)分析')
True
os.path.isdir('C:/Users/wuzhengxiang/Desktop/股票數(shù)據(jù)分析1')
False
os.path.isdir('C:/Users/wuzhengxiang/Desktop/股票數(shù)據(jù)分析/pi.txt')
False
30、os.path.join()
描述:把目錄和文件名合成一個路徑,1.如果各組件名首字母不包含’/’,則函數(shù)會自動加上,2.如果有一個組件是一個絕對路徑,則在它之前的所有組件均會被舍棄,3.如果最后一個組件為空,則生成的路徑以一個’/’分隔符結(jié)尾
語法:os.path.join(path1[, path2[, ...]])
os.path.join('C:/Users','wuzhengxiang/Desktop/','股票數(shù)據(jù)分析')
'C:/Users\\wuzhengxiang/Desktop/股票數(shù)據(jù)分析'
Path1 = 'home'
Path2 = 'develop'
Path3 = 'code'
Path10 = Path1 + Path2 + Path3
Path20 = os.path.join(Path1,Path2,Path3)
print ('Path10 = ',Path10)
print ('Path20 = ',Path20)
Path10 = homedevelopcode
Path20 = home\develop\code
31、os.path.normcase()
描述:轉(zhuǎn)換path的大小寫和斜杠
語法:os.path.normcase(path)
os.path.normcase('D:\Python\test\data.txt')
'd:\\python\test\\data.txt'
os.path.normcase('c:/WINDOWS\\system64\\')
'c:\\windows\\system64\\'
32、os.path.normpath()
描述:規(guī)范path字符串形式
語法:os.path.normpath(path)
os.path.normpath('c://windows\\System32\\../Temp/')
'c:\\windows\\Temp'
33、os.path.realpath()
描述:返回path的真實路徑
語法:os.path.realpath(path)
os.path.relpath('C:\\Users\\Administrat\\代碼TRY\\test.ipynb', '代碼TRY')
'..\\..\\..\\..\\Administrat\\代碼TRY\\test.ipynb'
34、os.path.relpath()
描述:返回從當(dāng)前目錄或 start 目錄(可選)到達(dá) path 之間要經(jīng)過的相對路徑。這僅僅是對路徑的計算,不會訪問文件系統(tǒng)來確認(rèn) path 或 start 的存在性或?qū)傩浴?
語法:os.path.relpath(path[, start])
os.path.relpath('C:/Users/wuzhengxiang/Desktop/股票數(shù)據(jù)分析\\test.gif')
'test.gif'
35、os.path.samefile( )
描述:判斷目錄或文件是否相同
語法:os.path.samefile(path1, path2)
os.path.samefile('C:\\Users', 'C:\\Users')True
os.path.samefile('C:\\Users', 'C:/Users')True
os.path.samefile('C:\\Users', 'C:/Users/wuzhengxiang')False
36、os.path.split()
描述:把路徑分割成 dirname 和 basename,返回一個元組
語法:os.path.split(path)
os.path.split('D:\Python\test\data.txt')
('D:\\Python\test', 'data.txt')
37、os.path.splitdrive()
37、os.path.splitdrive()
描述:一般用在 windows 下,返回驅(qū)動器名和路徑組成的元組
語法:os.path.splitdrive(path)
os.path.splitdrive('C:/Users/zhengxiang.wzx/IMG_7358.JPG')
('C:', '/Users/zhengxiang.wzx/IMG_7358.JPG')
38、os.path.splitext()
描述:分割路徑,返回路徑名和文件擴(kuò)展名的元組
語法:os.path.splitext(path)
os.path.splitext('C:/Users/zhengxiang.wzx/IMG_7358.JPG')
('C:/Users/zhengxiang.wzx/IMG_7358', '.JPG')
39、os.path.walk()
描述:遍歷path,進(jìn)入每個目錄都調(diào)用visit函數(shù),visit函數(shù)必須有3個參數(shù)(arg, dirname, names),dirname表示當(dāng)前目錄的目錄名,names代表當(dāng)前目錄下的所有文件名,args則為walk的第三個參數(shù)
語法:os.path.walk(path, visit, arg)
list(os.walk(abs_cur_dir))
[('C:/Users/wuzhengxiang/Desktop/股票數(shù)據(jù)分析',
['1122'],
['ETF研究.py', 'foo.txt', 'pi.txt', 'render.html', 'test.gif']),
('C:/Users/wuzhengxiang/Desktop/股票數(shù)據(jù)分析\\1122', [], [])]
#窮舉遍歷一個文件夾里面的所有文件,并獲取文件的目錄名
abs_cur_dir ='C:/Users/wuzhengxiang/Desktop/股票數(shù)據(jù)分析'
file_url=[]
for dirs,folders,files in os.walk(abs_cur_dir):
for i in files:
file_url.append(os.path.join(dirs,i))
file_url
['C:/Users/wuzhengxiang/Desktop/股票數(shù)據(jù)分析\\ETF研究.py',
'C:/Users/wuzhengxiang/Desktop/股票數(shù)據(jù)分析\\foo.txt',
'C:/Users/wuzhengxiang/Desktop/股票數(shù)據(jù)分析\\pi.txt',
'C:/Users/wuzhengxiang/Desktop/股票數(shù)據(jù)分析\\render.html',
'C:/Users/wuzhengxiang/Desktop/股票數(shù)據(jù)分析\\test.gif']
#pathlib也能實現(xiàn)類似的
數(shù)據(jù)分析咨詢請掃描二維碼
若不方便掃碼,搜微信號:CDAshujufenxi
SQL Server 中 CONVERT 函數(shù)的日期轉(zhuǎn)換:從基礎(chǔ)用法到實戰(zhàn)優(yōu)化 在 SQL Server 的數(shù)據(jù)處理中,日期格式轉(zhuǎn)換是高頻需求 —— 無論 ...
2025-09-18MySQL 大表拆分與關(guān)聯(lián)查詢效率:打破 “拆分必慢” 的認(rèn)知誤區(qū) 在 MySQL 數(shù)據(jù)庫管理中,“大表” 始終是性能優(yōu)化繞不開的話題。 ...
2025-09-18CDA 數(shù)據(jù)分析師:表結(jié)構(gòu)數(shù)據(jù) “獲取 - 加工 - 使用” 全流程的賦能者 表結(jié)構(gòu)數(shù)據(jù)(如數(shù)據(jù)庫表、Excel 表、CSV 文件)是企業(yè)數(shù)字 ...
2025-09-18DSGE 模型中的 Et:理性預(yù)期算子的內(nèi)涵、作用與應(yīng)用解析 動態(tài)隨機(jī)一般均衡(Dynamic Stochastic General Equilibrium, DSGE)模 ...
2025-09-17Python 提取 TIF 中地名的完整指南 一、先明確:TIF 中的地名有哪兩種存在形式? 在開始提取前,需先判斷 TIF 文件的類型 —— ...
2025-09-17CDA 數(shù)據(jù)分析師:解鎖表結(jié)構(gòu)數(shù)據(jù)特征價值的專業(yè)核心 表結(jié)構(gòu)數(shù)據(jù)(以 “行 - 列” 規(guī)范存儲的結(jié)構(gòu)化數(shù)據(jù),如數(shù)據(jù)庫表、Excel 表、 ...
2025-09-17Excel 導(dǎo)入數(shù)據(jù)含缺失值?詳解 dropna 函數(shù)的功能與實戰(zhàn)應(yīng)用 在用 Python(如 pandas 庫)處理 Excel 數(shù)據(jù)時,“缺失值” 是高頻 ...
2025-09-16深入解析卡方檢驗與 t 檢驗:差異、適用場景與實踐應(yīng)用 在數(shù)據(jù)分析與統(tǒng)計學(xué)領(lǐng)域,假設(shè)檢驗是驗證研究假設(shè)、判斷數(shù)據(jù)差異是否 “ ...
2025-09-16CDA 數(shù)據(jù)分析師:掌控表格結(jié)構(gòu)數(shù)據(jù)全功能周期的專業(yè)操盤手 表格結(jié)構(gòu)數(shù)據(jù)(以 “行 - 列” 存儲的結(jié)構(gòu)化數(shù)據(jù),如 Excel 表、數(shù)據(jù) ...
2025-09-16MySQL 執(zhí)行計劃中 rows 數(shù)量的準(zhǔn)確性解析:原理、影響因素與優(yōu)化 在 MySQL SQL 調(diào)優(yōu)中,EXPLAIN執(zhí)行計劃是核心工具,而其中的row ...
2025-09-15解析 Python 中 Response 對象的 text 與 content:區(qū)別、場景與實踐指南 在 Python 進(jìn)行 HTTP 網(wǎng)絡(luò)請求開發(fā)時(如使用requests ...
2025-09-15CDA 數(shù)據(jù)分析師:激活表格結(jié)構(gòu)數(shù)據(jù)價值的核心操盤手 表格結(jié)構(gòu)數(shù)據(jù)(如 Excel 表格、數(shù)據(jù)庫表)是企業(yè)最基礎(chǔ)、最核心的數(shù)據(jù)形態(tài) ...
2025-09-15Python HTTP 請求工具對比:urllib.request 與 requests 的核心差異與選擇指南 在 Python 處理 HTTP 請求(如接口調(diào)用、數(shù)據(jù)爬取 ...
2025-09-12解決 pd.read_csv 讀取長浮點數(shù)據(jù)的科學(xué)計數(shù)法問題 為幫助 Python 數(shù)據(jù)從業(yè)者解決pd.read_csv讀取長浮點數(shù)據(jù)時的科學(xué)計數(shù)法問題 ...
2025-09-12CDA 數(shù)據(jù)分析師:業(yè)務(wù)數(shù)據(jù)分析步驟的落地者與價值優(yōu)化者 業(yè)務(wù)數(shù)據(jù)分析是企業(yè)解決日常運(yùn)營問題、提升執(zhí)行效率的核心手段,其價值 ...
2025-09-12用 SQL 驗證業(yè)務(wù)邏輯:從規(guī)則拆解到數(shù)據(jù)把關(guān)的實戰(zhàn)指南 在業(yè)務(wù)系統(tǒng)落地過程中,“業(yè)務(wù)邏輯” 是連接 “需求設(shè)計” 與 “用戶體驗 ...
2025-09-11塔吉特百貨孕婦營銷案例:數(shù)據(jù)驅(qū)動下的精準(zhǔn)零售革命與啟示 在零售行業(yè) “流量紅利見頂” 的當(dāng)下,精準(zhǔn)營銷成為企業(yè)突圍的核心方 ...
2025-09-11CDA 數(shù)據(jù)分析師與戰(zhàn)略 / 業(yè)務(wù)數(shù)據(jù)分析:概念辨析與協(xié)同價值 在數(shù)據(jù)驅(qū)動決策的體系中,“戰(zhàn)略數(shù)據(jù)分析”“業(yè)務(wù)數(shù)據(jù)分析” 是企業(yè) ...
2025-09-11Excel 數(shù)據(jù)聚類分析:從操作實踐到業(yè)務(wù)價值挖掘 在數(shù)據(jù)分析場景中,聚類分析作為 “無監(jiān)督分組” 的核心工具,能從雜亂數(shù)據(jù)中挖 ...
2025-09-10統(tǒng)計模型的核心目的:從數(shù)據(jù)解讀到?jīng)Q策支撐的價值導(dǎo)向 統(tǒng)計模型作為數(shù)據(jù)分析的核心工具,并非簡單的 “公式堆砌”,而是圍繞特定 ...
2025-09-10