1. 程式人生 > >python基礎:os模組

python基礎:os模組

import os
1.檔案重新命名 rename
第一個引數是原檔名,第二個引數是新檔名

os.rename('file/b.txt','bb.txt')     將原本file檔案的子模組b.txt移動到了file同級目錄,相當於剪下移動

2、刪除檔案,不能刪除資料夾 remove

os.remove('bb.txt')       將移動後的bb.txt檔案刪除

3、 建立目錄
建立一層目錄 mkdir

os.mkdir('file01')     在file同級目錄建立了一個新的目錄file01

4.建立多層目錄 makedirs

os.makedirs('file03/file04/file05')
# 遞迴創造目錄,如果目錄已存在,報錯
os.makedirs('file04/file05',exist_ok=True)
#此種情況目錄已經存在,不會報錯,將不會再建立

5.刪除目錄
os.rmdir 刪除一層空目錄
如果目錄不為空目錄,將會報錯

os.rmdir('file01/file02')
os.rmdir('file03/file04/file05')

6.removedirs 刪除多層空目錄,如果目錄裡面有內容則不會刪除

  os.removedirs('file03/file04/file05')

7.getcwd 獲取當前檔案所在目錄

ret=os.getcwd()
print(ret)           ##E:\homework\pythonworksplace\day13

8.listdir 獲取目錄列表

ret=os.listdir(os.getcwd())
print(ret)    返回列表形式

執行結果:

['.idea', 'b.txt', 'bbb.txt', 'demon01-檔案許可權操作.py', 'demon02-讀操作.py', 'demon03-寫操作.py', 'demon04-seek方法.py', 'demon05-with方式.py', 'demon06-亂碼.py', 'demon07-閱讀電子書翻頁.py', 'demon08-os模組.py', 'demon09-學生管理系統.py', 'file', 'file01', 'file03']

9.切換所在目錄 chdir()

os.chdir(os.getcwd()+'/file')     # lin
os.chdir(os.getcwd()+'\\file')#windows系統下路徑分隔符
print(os.getcwd())      #E:\homework\pythonworksplace\day13\file

10.判斷檔案或資料夾是否存在

ret=os.path.exists('file')     file是資料夾返回True
print(ret)      #True
ret1=os.path.exists('b.txt')    返回True
print(ret1)     #False

11。判斷是否為檔案

判斷所傳路徑是否為檔案,是返回True,不是返回False

ret=os.path.isfile('file/c.txt')   
print(ret)    True
ret=os.path.isfile('c.txt')    c.txt與正在執行的檔案不在同一目錄,判斷時,將只會從同級目錄找尋,此時找不到c.txt,返回False
print(ret)   False

12.判斷是否為目錄

ret=os.path.isdir('b.txt')
print(ret)        #False       b.txt不是目錄,返回False
ret1=os.path.isdir('file')
print(ret1)      #True

13.獲取絕對路徑,從磁碟開始找

    ret=os.path.abspath('file')
    print(ret)     #E:\homework\pythonworksplace\day13\file
    ret1=os.getcwd()   #獲取當前檔案所在目錄
    print(ret1)    #E:\homework\pythonworksplace\day13
    ret=os.path.isabs(ret1)
    print(ret1)    #E:\homework\pythonworksplace\day13
    ret=os.path.isabs(ret1)
    print(ret)      #True

14.判斷是否為絕對路徑
.

ret=os.path.isabs('file')  #file為當前檔案所在的目錄,非絕對路徑
print(ret)      #False
ret1=os.getcwd()    #獲取當前檔案所在目錄
print(ret1)    #E:\homework\pythonworksplace\day13
ret=os.path.isabs(ret1)   
print(ret)      #True

15.獲取路徑中最後部分

ret=os.path.abspath('file03/file04/file05')
print(ret)   #E:\homework\pythonworksplace\day13\file03\file04\file05
ret=os.path.basename('file03/file04/file05')
print(ret)   #file05

16.獲取路徑中父目錄部分,不管最後是檔案還是資料夾。

    #獲取當前檔案所在目錄的路徑的上一層
    print(os.getcwd())     #E:\homework\pythonworksplace\day13
    ret=os.path.dirname(os.getcwd())
    print(ret)        #E:\homework\pythonworksplace
    ret1=ret=os.path.abspath('demon08-os模組')
    #獲取當前檔案的絕對路徑的上一層
    print(ret1)     #E:\homework\pythonworksplace\day13\demon08-os模組
    ret2=os.path.dirname(ret1)
    print(ret2)      #E:\homework\pythonworksplace\day13
	獲取當前檔案絕對路徑
	print(__file__)#E:\homework\pythonworksplace\day13\demon08-os模組
	ret=os.path.dirname(__file__)

17.將多個目錄組織成路徑返回

ret=os.path.join('aa','bb','cc')
print(ret)   # aa\bb\cc

18:
(1).getatime()返回path所指向的檔案或者目錄的最後訪問時間

 ret=os.path.getatime('file')
 print(ret)    # 1547101024.0272446  返回時間戳

(2).getctime()檢視檔案建立時間

ret=os.path.getctime('file')
print(ret)    #1547083130.1095338

(3).getmtime()返回path所指向的檔案或者目錄的最後修改時間

 ret=os.path.getmtime('file')
 print(ret)      #1547101024.0272446

(4).getsize()檢視檔案的大小
c.txt檔案內容為:你好,世界

 ret=os.path.getsize('file/c.txt')
 print(ret)      #13