Python開發(fā)之os與os.path的使用小結(jié)
1. os的一般用法
使用dir()列出庫的屬性與方法
# 使用dir()列出庫的屬性與方法 print(dir(os))
使用os.getcwd()打印當(dāng)前目錄
# 使用os.getcwd()打印當(dāng)前目錄 print("當(dāng)前目錄為:"+os.getcwd()) # 打印表示當(dāng)前工作目錄的字符串
獲取指定路徑下的目錄和文件列表
# 獲取指定路徑下的目錄和文件列表 root = r'/path/to/your/directory' # 絕對路徑 path = os.listdir(root) print(path)
創(chuàng)建目錄
# 創(chuàng)建目錄 os.makedirs(r'B\C', exist_ok=True) # 方法1.利用makedirs()創(chuàng)建多級目錄 os.mkdir(r'A') # 方法2.利用mkdir()創(chuàng)建單級目錄
以當(dāng)前目錄作為相對目錄的基準,在相對目錄下的A中創(chuàng)建message.txt
# 以當(dāng)前目錄作為相對目錄的基準,在相對目錄下的A中創(chuàng)建message.txt with open(r"A\message.txt", "w")as file: pass
刪除空目錄
# 刪除空目錄 os.removedirs(r"B\C") # 使用removedirs()遞歸刪除目錄。 # os.rmdir(r"B\C") # 刪除目錄
刪除文件
# 刪除文件 os.remove(r'A\message.txt')
完全刪除一個目錄以及所有內(nèi)容
# 完全刪除一個目錄以及所有內(nèi)容 import shutil dir_path = "/path/to/your/directory" # 將此處的路徑替換為你要刪除的目錄路徑 try: shutil.rmtree(dir_path) print("目錄已成功刪除。") except OSError as e: print("刪除目錄時出錯:", e)
2. os.path的用法
使用os.path.abspath() 打印"A\message.txt"的絕對路徑
# 使用os.path.abspath()打印"A\message.txt"的絕對路徑 print("message.txt的絕對路徑為:"+os.path.abspath(r"A\message.txt")) # "A\message.txt"換為你的相對路徑下的路徑 print("運行文件的絕對路徑為:"+os.path.abspath(__file__)) # 當(dāng)前路徑
os.path.exists(path) 判斷該路徑或文件是否存在
# os.path.exists(path) 判斷該路徑或文件是否存在 print(os.path.exists(r'/path/to/your/directory')) # 檢查A\message.txt文件是否存在 print(os.path.exists(r'A\message.txt'))
os.path.dirname() 方法用于從指定路徑中獲取目錄名,返回上一級的目錄
# os.path.dirname() 方法用于從指定路徑中獲取目錄名,返回上一級的目錄 # Path path = r'\path\to\your\directory\A' # 獲取指定路徑下的目錄名 dirname = os.path.dirname(path) # 打印目錄名稱 print(dirname) # Path path = r'\path\to\your\directory\A\message.txt' # 獲取指定路徑下的目錄名 dirname = os.path.dirname(path) # 打印目錄名稱 print(dirname)
os.path.join() 拼接路徑
# os.path.join()拼接路徑 Path1 = 'home' Path2 = 'develop' Path3 = '' Path10 = Path1 + Path2 + Path3 Path20 = os.path.join(Path1, Path2, Path3) Path30 = os.path.join(Path2, Path3, Path1) print('Path10 = ', Path10) print('Path20 = ', Path20) print('Path30 = ', Path30)
3. 完整代碼
如下:
# -*- coding: utf-8 -*- # @Time : 23/05/2024 09:47 # @Author : jin # @File : ex1.py # @Project : python exercises # @Descriptioon : # @path : \Users\ jin\add_yourpath\ python exercises import os # os的一般用法 # 使用dir()列出庫的屬性與方法 print(dir(os)) # 使用os.getcwd()打印當(dāng)前目錄 print("當(dāng)前目錄為:"+os.getcwd()) # 打印表示當(dāng)前工作目錄的字符串 # 獲取指定路徑下的目錄和文件列表 root = r'/path/to/your/directory' # 絕對路徑 path = os.listdir(root) print(path) # 創(chuàng)建目錄 os.makedirs(r'B\C', exist_ok=True) # 方法1.利用makedirs()創(chuàng)建多級目錄 os.mkdir(r'A') # 方法2.利用mkdir()創(chuàng)建單級目錄 # 以當(dāng)前目錄作為相對目錄的基準,在相對目錄下的A中創(chuàng)建message.txt with open(r"A\message.txt", "w")as file: pass # 刪除空目錄 os.removedirs(r"B\C") # 使用removedirs()遞歸刪除目錄。 # os.rmdir(r"B\C") # 刪除目錄 # 刪除文件 os.remove(r'A\message.txt') # 完全刪除一個目錄以及所有內(nèi)容 # import shutil # # dir_path = "/path/to/your/directory" # 將此處的路徑替換為你要刪除的目錄路徑 # # try: # shutil.rmtree(dir_path) # print("目錄已成功刪除。") # except OSError as e: # print("刪除目錄時出錯:", e) # 以當(dāng)前目錄作為相對目錄的基準,在相對目錄下的A中創(chuàng)建message.txt with open(r"A\message.txt", "w"): pass """-----------------------------------------------------------------------""" # os.path的用法 # 使用os.path.abspath()打印"A\message.txt"的絕對路徑 print("message.txt的絕對路徑為:"+os.path.abspath(r"A\message.txt")) # "A\message.txt"換為你的相對路徑下的路徑 print("運行文件的絕對路徑為:"+os.path.abspath(__file__)) # 當(dāng)前路徑 # os.path.exists(path) 判斷該路徑或文件是否存在 print(os.path.exists(r'/path/to/your/directory')) # 檢查A\message.txt文件是否存在 print(os.path.exists(r'A\message.txt')) # os.path.dirname() 方法用于從指定路徑中獲取目錄名 # Path path = r'\path\to\your\directory\A' # Get the directory name from the specified path dirname = os.path.dirname(path) # Print the directory name print(dirname) # Path path = r'\path\to\your\directory\A\message.txt' # Get the directory name from the specified path dirname = os.path.dirname(path) # Print the directory name print(dirname) # os.path.join()拼接路徑 Path1 = 'home' Path2 = 'develop' Path3 = '' Path10 = Path1 + Path2 + Path3 Path20 = os.path.join(Path1, Path2, Path3) Path30 = os.path.join(Path2, Path3, Path1) print('Path10 = ', Path10) print('Path20 = ', Path20) print('Path30 = ', Path30)
到此這篇關(guān)于Python開發(fā)之os與os.path的使用小結(jié)的文章就介紹到這了,更多相關(guān)python os與os.path的使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python實現(xiàn)PPT幻燈片的添加、刪除或隱藏操作
PowerPoint文檔是商務(wù)、教育、創(chuàng)意等各領(lǐng)域常見的用于展示、教育和傳達信息的格式,在制作PPT演示文稿時,靈活地操作幻燈片是提高演示效果、優(yōu)化內(nèi)容組織的關(guān)鍵步驟,本文給大家介紹了Python 操作PPT幻燈片- 添加、刪除、或隱藏幻燈片,需要的朋友可以參考下2024-08-08Python使用cProfile分析和定位應(yīng)用性能瓶頸點
cProfile?是?Python?標準庫中的一個模塊,用于對?Python?程序進行性能分析,它能輸出每個函數(shù)的調(diào)用次數(shù)、執(zhí)行耗時等詳細信息,下面我們來看看如何使用cProfile分析和定位應(yīng)用性能瓶頸點吧2024-12-12