Python - os庫


os簡單介紹

 os 模塊提供了非常豐富的方法用來處理文件和目錄

 

os關於目錄路徑的方法

 1 # 獲取當前路徑
 2 path = os.getcwd()
 3 
 4 # 獲取當前絕對路徑
 5 os.path.abspath(path)
 6 
 7 # 創建一級目錄
 8 os.mkdir(path)
 9 
10 # 刪除空目錄
11 os.rmdir(path)
12 
13 # 創建多級目錄
14 os.makedirs(path)
15 
16 # 刪除多級空目錄
17 os.removedirs(path)
18 
19 # 修改路徑為path
20 os.chdir(path)

包含知識點

  • rmdir的path,如果目錄非空,則拋出一個OSError異常
  • 多級目錄是指 /test/testing/test,如果三個目錄都不存在則都會創建

 

os關於文件的方法

# 獲取當前路徑下所有文件、文件夾
os.listdir(path)

# 創建文件方式一
f = os.open(path + "test.txt", flags=os.O_CREAT | os.O_RDWR )

# 寫入文件
os.write(f, bytes("123",encoding="utf-8"))

# 讀取文件
print(os.read(f,12))

# 關閉文件
os.close(f)

# 重命名文件
os.rename(path + "test.txt", path + "tests.txt")

# 刪除文件
os.remove(path + "tests.txt")
# 遞歸返回path下的目錄(包括path目錄)、子目錄、文件名的三元組 for root, dirname, filenames in os.walk(path): logzeros.debug(root) logzeros.debug(dirname) logzeros.debug(filenames)

包含知識點

listdir 返回的是一個列表,若沒有文件則返回空列表

os.write(fd, str) 用於寫入bytes字符串到文件描述符 fd 中. 返回實際寫入的字符串長度

os.read(fd,n) 用於從文件描述符 fd 中讀取最多 n 個字節,返回包含bytes字符串

關於 open() 可看此博客:https://www.cnblogs.com/poloyy/p/12350158.html 

關於 os.walk(path) 可看此博客:https://www.cnblogs.com/poloyy/p/12349230.html 

 

os.path相關

os.path.realpath(__file__) 

獲取當前文件所在目錄

path = os.path.realpath(__file__)
print(path)

運行結果

C:\Users\user\Desktop\py\moocInterface\learn\os_path_learn.py

 

os.path.abspath(path) 

獲取當前path所在路徑

path = os.path.abspath(".")
print(path)

path = os.path.abspath(os.path.realpath(__file__))
print(path)

運行結果

C:\Users\user\Desktop\py\moocInterface\learn
C:\Users\user\Desktop\py\moocInterface\learn\os_path_learn.py

第一行代碼跟 os.getcwd() 很像

path = os.getcwd()
print(path)

運行結果

C:\Users\user\Desktop\py\moocInterface\learn

 

os.path.dirname(path) 

返回path的所在目錄的路徑

print(os.path.dirname(r'C:\Users\user\Desktop\py\moocInterface\learn\os_path_learn.py'))

print(os.path.dirname(r'C:\Users\user\Desktop\py\moocInterface\learn'))

# 表示獲取當前文件所在目錄的上一級目錄,即項目所在目錄C:\Users\user\Desktop\py\moocInterface
print(os.path.dirname(os.path.abspath('.'))) 

運行結果

C:\Users\user\Desktop\py\moocInterface\learn
C:\Users\user\Desktop\py\moocInterface
C:\Users\user\Desktop\py\moocInterface

 

os.path.split(path) 

分離文件名和擴展名,返回(filename文件名,fileextension文件擴展名)二元組

# 目錄
os.path.split(os.getcwd())

# 文件
os.path.split(os.path.realpath(__file__))

運行結果

('C:\\Users\\user\\Desktop\\py\\moocInterface', 'learn')
('C:\\Users\\user\\Desktop\\py\\moocInterface\\learn', 'os_path_learn.py')

 

os.path.join()

用於路徑拼接,將多個路徑組合后返回,第一個絕對路徑之前的參數將被忽略

# 拼接目錄
new_path = os.path.join(os.getcwd(), "test")
print(new_path)

# 拼接文件
new_path = os.path.join(os.getcwd(), "test.txt")
print(new_path)

# 拼接多重目錄
new_path = os.path.join(os.getcwd(), "test/test/test")
print(new_path)

# 拼接多個目錄、文件
new_path = os.path.join(os.getcwd(), "test", "Test", "ok.txt")
print(new_path)

運行結果

C:\Users\user\Desktop\py\moocInterface\learn\test
C:\Users\user\Desktop\py\moocInterface\learn\test.txt
C:\Users\user\Desktop\py\moocInterface\learn\test/test/test
C:\Users\user\Desktop\py\moocInterface\learn\test\Test\ok.txt

 

 


免責聲明!

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



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