網站首頁 編程語言 正文
os模塊封裝了常見的文件和目錄操作
導入模塊
import os
常用方法
os.rename(path,new_path_name)
os.getcwd(path):返回當前工作目錄;
print(os.getcwd()) # C:\Users\ThinkStation\Desktop
os.listdir(path=“.”):返回文件夾下所有文件名
path = 'C:/Users/ThinkStation/Desktop/test'
print(os.listdir(path)) # ['test_data.xlsx', '文件', '海報', '蘋果']
os.path.exists(path):檢查文件或文件夾是否存在 os.mkdir(path):創建文件夾
print(os.path.exists(path))#True
path_new='C:/Users/ThinkStation/Desktop/test_new'
if os.path.exists(path_new):
os.mkdir(path_new)
os.remove(path):刪除一個文件
os.rmdir(path):刪除一個文件夾
os.replace(src, dst):重命名或替換文件
os.walk(path):遍歷獲取所有下級文件和文件的文件名
for i in os.walk(path):
print(i)
'''
('C:/Users/ThinkStation/Desktop/test', ['文件', '海報', '蘋果'], ['test_data.xlsx'])
('C:/Users/ThinkStation/Desktop/test\\文件', [], ['main.py', 'manage.py', '草稿本.docx'])
('C:/Users/ThinkStation/Desktop/test\\海報', [], ['18.jpg', '19.jpg', '20.jpg', '21.jpg'])
('C:/Users/ThinkStation/Desktop/test\\蘋果', [], ['0.jpg', '1.jpg', '2.jpg', '3.jpg', '4.jpg'])
'''
os.path.abspath(path):返回絕對路徑
path_r = './test'
print(os.path.abspath(path_r))#C:\Users\ThinkStation\Desktop\test
os.path.dirname(path):返回文件夾名稱 os.path.getsize(path):返回文件/文件夾的大小,以字節為單位
path2='C:/Users/ThinkStation/Desktop/airplane-expend'
print( os.path.getsize(path2))#393216
os.path.isabs(path):判斷路徑是否是一個絕對路徑
path = 'C:/Users/ThinkStation/Desktop/test'
path_r = './test'
print(os.path.isabs(path_r))#False
print(os.path.isabs(path))#True
os.path.isfile(path):判斷是否是一個文件
os.path.isdir(path):判斷是否是一個文件夾
print(os.path.isfile(path))#False
print(os.path.isdir(path))#True
os.path.join(path, *path):智能拼接文件路徑
拼接方式1:直接+
path3 = 'C:/Users/ThinkStation/Desktop'
path4='test'
filepath = path3 + '/' + path4
print(filepath)#C:/Users/ThinkStation/Desktop/test
print(os.path.join(path3,path4))#C:/Users/ThinkStation/Desktop/test
拼接方式2:os.path.join
print(os.path.join(path3,path4))#C:/Users/ThinkStation/Desktop\test
os.path.split(path):拆分文件路徑
print(os.path.split(path))#('C:/Users/ThinkStation/Desktop', 'test')
原文鏈接:https://blog.csdn.net/xiaofeixia002X/article/details/128463738
相關推薦
- 2022-07-11 Sonatype Nexus搭建Maven私服
- 2022-09-05 Spark Sql之count(distinct)分析&&學習&&驗
- 2022-07-18 RestTemplate轉發MultipartFile
- 2022-06-16 golang默認Logger日志庫在項目中使用Zap日志庫_Golang
- 2022-11-19 ElasticSearch事件查詢語言EQL操作_服務器其它
- 2022-07-26 C++智能指針之shared_ptr的具體使用_C 語言
- 2021-12-22 Docker部署Microsoft?Sql?Server詳細步驟_docker
- 2022-11-29 React?中?memo?useMemo?useCallback?到底該怎么用_React
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支