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)前目錄作為相對目錄的基準(zhǔn),在相對目錄下的A中創(chuàng)建message.txt
# 以當(dāng)前目錄作為相對目錄的基準(zhǔn),在相對目錄下的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)前目錄作為相對目錄的基準(zhǔn),在相對目錄下的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)前目錄作為相對目錄的基準(zhǔn),在相對目錄下的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)文章
windows系統(tǒng)下anaconda的安裝和使用方式
Anaconda是集成Python/R數(shù)據(jù)科學(xué)工具的開源平臺,通過conda包管理器簡化環(huán)境配置與版本沖突問題,安裝需下載并設(shè)置環(huán)境變量,常用指令包括環(huán)境創(chuàng)建、激活、刪除及使用鏡像源安裝包2025-09-09
python在windows和linux下獲得本機本地ip地址方法小結(jié)
這篇文章主要介紹了python在windows和linux下獲得本機本地ip地址方法,實例分析了Python獲得IP地址的技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-03-03
python使用requests實現(xiàn)發(fā)送帶文件請求功能
這篇文章主要介紹了python使用requests實現(xiàn)發(fā)送帶文件請求,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-12-12

