日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【获取路径grob()】学习笔记

發布時間:2023/12/20 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【获取路径grob()】学习笔记 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

最近新接觸了一個獲取路徑的方法—grob()。這個方法可以生成一個迭代器,里面會有文件比較全面的信息。并且使用簡單,可以避免之前繁瑣的提取,拼組路徑的操作。

目錄文件結構

GLOB │ glob_test.py │ └───test|└───littkt_rain│ │ 20201010.h|└───your_name| 520| 666.txt

glob的使用

Path('XXX').rglob('*') # 迭代遍歷文件夾下所有文件 Path('XXX').glob('*') # 遍歷文子件夾下的文件

其中XXX表示路徑,glob的參數是正則表達式表達的文件類型,*代表任意文件

glob_test.py

# -*- coding: utf-8 -*- from pathlib import PathSCRIPT_DIR = Path(__file__).parent # 獲取當前路徑 print(SCRIPT_DIR, '\r\n')for file in Path('test').rglob('*'): # 迭代遍歷文件夾下所有文件print((file, file.parent, file.name, file.suffix)) print('') for file in Path('test').rglob('*.txt'): # 獲取.txt文件print('FILE:', file, '\r\n') for file in Path('test').glob('*'): # 遍歷文子件夾下的文件print('FILE:', file)

運行結果

C:\Users\86153\Desktop\GLOB (WindowsPath('test/little_rain'), WindowsPath('test'), 'little_rain', '') (WindowsPath('test/your_name'), WindowsPath('test'), 'your_name', '') (WindowsPath('test/little_rain/20201010.h'), WindowsPath('test/little_rain'), '20201010.h', '.h') (WindowsPath('test/your_name/520'), WindowsPath('test/your_name'), '520', '') (WindowsPath('test/your_name/666.txt'), WindowsPath('test/your_name'), '666.txt', '.txt')FILE: test\your_name\666.txt FILE: test\little_rain FILE: test\your_name

總結

以上是生活随笔為你收集整理的【获取路径grob()】学习笔记的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。