日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区

您的位置:首頁技術文章
文章詳情頁

python中pathlib模塊的基本用法與總結

瀏覽:115日期:2022-07-13 18:56:59

前言

相比常用的 os.path而言,pathlib 對于目錄路徑的操作更簡介也更貼近 Pythonic。但是它不單純是為了簡化操作,還有更大的用途。

pathlib 是Python內置庫,Python 文檔給它的定義是:The pathlib module ? object-oriented filesystem paths(面向對象的文件系統路徑)。pathlib 提供表示文件系統路徑的類,其語義適用于不同的操作系統。

python中pathlib模塊的基本用法與總結

更多詳細的內容可以參考官方文檔:https://docs.python.org/3/library/pathlib.html#methods

1. pathlib模塊下Path類的基本使用

from pathlib import Pathpath = r’D:pythonpycharm2020programpathlib模塊的基本使用.py’p = Path(path)print(p.name) # 獲取文件名print(p.stem) # 獲取文件名除后綴的部分print(p.suffix) # 獲取文件后綴print(p.parent) # 相當于dirnameprint(p.parent.parent.parent)print(p.parents) # 返回一個iterable 包含所有父目錄for i in p.parents: print(i)print(p.parts) # 將路徑通過分隔符分割成一個元組

運行結果如下:

pathlib模塊的基本使用.pypathlib模塊的基本使用.pyD:pythonpycharm2020programD:python<WindowsPath.parents>D:pythonpycharm2020programD:pythonpycharm2020D:pythonD:(’D:’, ’python’, ’pycharm2020’, ’program’, ’pathlib模塊的基本使用.py’)

Path.cwd():Return a new path object representing the current directory Path.home():Return a new path object representing the user’s home directory Path.expanduser():Return a new path with expanded ~ and ~user constructs

from pathlib import Pathpath_1 = Path.cwd() # 獲取當前文件路徑path_2 = Path.home()p1 = Path(’~/pathlib模塊的基本使用.py’)print(path_1)print(path_2)print(p1.expanduser())

運行結果如下:

D:pythonpycharm2020programC:UsersAdministratorC:UsersAdministratorpathlib模塊的基本使用.py

Path.stat():Return a os.stat_result object containing information about this path

from pathlib import Pathimport datetimep = Path(’pathlib模塊的基本使用.py’)print(p.stat()) # 獲取文件詳細信息print(p.stat().st_size) # 文件的字節大小print(p.stat().st_ctime) # 文件創建時間print(p.stat().st_mtime) # 上次修改文件的時間creat_time = datetime.datetime.fromtimestamp(p.stat().st_ctime)st_mtime = datetime.datetime.fromtimestamp(p.stat().st_mtime)print(f’該文件創建時間:{creat_time}’)print(f’上次修改該文件的時間:{st_mtime}’)

運行結果如下:

os.stat_result(st_mode=33206, st_ino=3659174698076635, st_dev=3730828260, st_nlink=1, st_uid=0, st_gid=0, st_size=543, st_atime=1597366826, st_mtime=1597366826, st_ctime=1597320585)5431597320585.76574751597366826.9711637該文件創建時間:2020-08-13 20:09:45.765748上次修改該文件的時間:2020-08-14 09:00:26.971164

從不同.stat().st_屬性 返回的時間戳表示自1970年1月1日以來的秒數,可以用datetime.fromtimestamp將時間戳轉換為有用的時間格式。

Path.exists():Whether the path points to an existing file or directoryPath.resolve(strict=False):Make the path absolute,resolving any symlinks. A new path object is returned

from pathlib import Pathp1 = Path(’pathlib模塊的基本使用.py’) # 文件p2 = Path(r’D:pythonpycharm2020program’) # 文件夾 absolute_path = p1.resolve()print(absolute_path)print(Path(’.’).exists())print(p1.exists(), p2.exists())print(p1.is_file(), p2.is_file())print(p1.is_dir(), p2.is_dir())print(Path(’/python’).exists())print(Path(’non_existent_file’).exists())

運行結果如下:

D:pythonpycharm2020programpathlib模塊的基本使用.pyTrueTrue TrueTrue FalseFalse TrueTrueFalse

Path.iterdir():When the path points to a directory,yield path objects of the directory contents

from pathlib import Pathp = Path(’/python’)for child in p.iterdir(): print(child)

運行結果如下:

pythonAnacondapythonEVCapturepythonEvernote_6.21.3.2048.exepythonNotepad++pythonpycharm-community-2020.1.3.exepythonpycharm2020pythonpyecharts-assets-masterpythonpyecharts-gallery-masterpythonSublime text 3

Path.glob(pattern):Glob the given relative pattern in the directory represented by this path, yielding all matching files (of any kind),The “**” pattern means “this directory and all subdirectories, recursively”. In other words, it enables recursive globbing.

Note:Using the “**” pattern in large directory trees may consume an inordinate amount of time

遞歸遍歷該目錄下所有文件,獲取所有符合pattern的文件,返回一個generator。

獲取該文件目錄下所有.py文件

from pathlib import Pathpath = r’D:pythonpycharm2020program’p = Path(path)file_name = p.glob(’**/*.py’)print(type(file_name)) # <class ’generator’>for i in file_name: print(i)

獲取該文件目錄下所有.jpg圖片

from pathlib import Pathpath = r’D:pythonpycharm2020program’p = Path(path)file_name = p.glob(’**/*.jpg’)print(type(file_name)) # <class ’generator’>for i in file_name: print(i)

獲取給定目錄下所有.txt文件、.jpg圖片和.py文件

from pathlib import Pathdef get_files(patterns, path): all_files = [] p = Path(path) for item in patterns: file_name = p.rglob(f’**/*{item}’) all_files.extend(file_name) return all_filespath = input(’>>>請輸入文件路徑:’)results = get_files([’.txt’, ’.jpg’, ’.py’], path)print(results)for file in results: print(file)

Path.mkdir(mode=0o777, parents=False, exist_ok=False)

Create a new directory at this given path. If mode is given, it is combined with the process’ umask value to determine the file mode and access flags. If the path already exists, FileExistsError is raised. If parents is true, any missing parents of this path are created as needed; they are created with the default permissions without taking mode into account (mimicking the POSIX mkdir -p command). If parents is false (the default), a missing parent raises FileNotFoundError. If exist_ok is false (the default), FileExistsError is raised if the target directory already exists. If exist_ok is true, FileExistsError exceptions will be ignored (same behavior as the POSIX mkdir -p command), but only if the last path component is not an existing non-directory file.

Changed in version 3.5: The exist_ok parameter was added.

Path.rmdir():Remove this directory. The directory must be empty.

from pathlib import Pathp = Path(r’D:pythonpycharm2020programtest’)p.mkdir()p.rmdir()

from pathlib import Pathp = Path(r’D:pythontest1test2test3’)p.mkdir(parents=True) # If parents is true, any missing parents of this path are created as neededp.rmdir() # 刪除的是test3文件夾

from pathlib import Pathp = Path(r’D:pythontest1test2test3’)p.mkdir(exist_ok=True) Path.unlink(missing_ok=False):Remove this file or symbolic link. If the path points to a directory, use Path.rmdir() instead. If missing_ok is false (the default), FileNotFoundError is raised if the path does not exist. If missing_ok is true, FileNotFoundError exceptions will be ignored. Changed in version 3.8:The missing_ok parameter was added. Path.rename(target):Rename this file or directory to the given target, and return a new Path instance pointing to target. On Unix, if target exists and is a file, it will be replaced silently if the user has permission. target can be either a string or another path object. Path.open(mode=‘r’, buffering=-1, encoding=None, errors=None, newline=None):Open the file pointed to by the path, like the built-in open() function does.

from pathlib import Pathp = Path(’foo.txt’)p.open(mode=’w’).write(’some text’)target = Path(’new_foo.txt’)p.rename(target)content = target.open(mode=’r’).read()print(content)target.unlink()

2. 與os模塊用法的對比

python中pathlib模塊的基本用法與總結

總結

到此這篇關于python中pathlib模塊的基本用法與總結的文章就介紹到這了,更多相關python pathlib模塊用法內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Python 編程
相關文章:
日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区
婷婷综合网站| 天堂成人国产精品一区| 亚洲三级国产| 亚洲免费影视| 午夜精品婷婷| 欧美日韩国产在线一区| 久久精品观看| 伊人精品一区| 91综合视频| 91免费精品| 亚洲a在线视频| av不卡在线看| 欧美资源在线| 在线看片一区| 国产精品一区二区三区四区在线观看 | 久久精品免费一区二区三区 | 91精品精品| 91精品在线观看国产| 亚洲先锋成人| 国产婷婷精品| 亚洲一区av| 青草国产精品| 国产精品日韩精品在线播放 | 蜜臀91精品一区二区三区| 日韩精品一二区| 亚洲另类av| 国产欧美日韩一级| 91综合视频| 久久视频国产| 亚洲精品电影| 亚洲乱亚洲高清| 国产精品国产三级在线观看| 97精品中文字幕| 99久久亚洲精品蜜臀| 日韩制服丝袜av| 国产欧美日韩亚洲一区二区三区| 精品国产aⅴ| 久久久9色精品国产一区二区三区| 亚洲在线网站| 久久国产精品色av免费看| 精品国产精品国产偷麻豆| 色乱码一区二区三区网站| 久久精品一本| 亚洲精品99| 日韩高清在线一区| 久久精品天堂| 在线亚洲自拍| 国产精品麻豆成人av电影艾秋 | 欧美成a人片免费观看久久五月天| 精精国产xxxx视频在线野外| 蜜桃一区二区三区在线| 国产美女精品视频免费播放软件| 精品视频一区二区三区四区五区| 欧美sss在线视频| 人人精品久久| 国产黄大片在线观看| 狠狠久久婷婷| 91欧美极品| 性欧美videohd高精| 伊人久久亚洲| 亚洲黄色免费av| 日本精品在线播放| 欧美日韩精品免费观看视完整| 亚洲精品第一| 亚洲大片在线| 国产欧美一区二区精品久久久 | 五月天综合网站| 久久狠狠久久| 99国产精品视频免费观看一公开| 国产欧美日韩影院| 欧美日韩国产亚洲一区| 粉嫩av一区二区三区四区五区 | 日本免费久久| 日韩国产在线一| 欧美日韩国产在线观看网站| 国产精品多人| 免费看黄色91| 亚洲手机在线| 久久精品资源| 亚洲一二三区视频| 国产欧美啪啪| 最新国产拍偷乱拍精品| 欧美aⅴ一区二区三区视频| 亚洲自啪免费| 精品高清久久| 在线视频精品| 日韩在线高清| 鲁大师精品99久久久| 视频一区欧美精品| 久久精品国产亚洲夜色av网站| 国产乱子精品一区二区在线观看| 夜久久久久久| 久久精品中文| 国产精品原创| 欧美激情在线精品一区二区三区| 亚洲一区二区毛片| 尤物tv在线精品| 欧美久久亚洲| 9色国产精品| 亚洲激精日韩激精欧美精品| 国产精东传媒成人av电影| 深夜福利亚洲| 中文日韩欧美| 久久91导航| 欧美国产专区| 婷婷成人av| 中文字幕日韩欧美精品高清在线| 日韩一区二区中文| 国产精品资源| 国产日韩欧美一区| 91精品国产自产精品男人的天堂| 免费不卡在线观看| 爽好多水快深点欧美视频| 999国产精品永久免费视频app| 日本在线高清| 国产美女撒尿一区二区| 日韩国产成人精品| 日韩有码av| 亚洲精品成a人ⅴ香蕉片| 免费不卡在线视频| 视频一区视频二区中文字幕| 亚洲免费影院| 蜜臀91精品一区二区三区| 日韩中文字幕区一区有砖一区| 亚洲在线电影| 一区二区国产精品| 亚洲精品观看| 日韩国产精品久久久久久亚洲| 日韩精品久久理论片| 欧美日韩在线精品一区二区三区激情综合| 欧美天堂亚洲电影院在线观看| 亚洲先锋成人| 日韩一区二区久久| 三级欧美在线一区| 日韩专区视频网站| 国产乱码精品一区二区三区亚洲人| 国产视频一区二| 国产精品videossex| 国产在线不卡一区二区三区| 色在线视频观看| 欧美午夜精彩| 亚洲少妇在线| 中文字幕日本一区二区| 欧美日韩 国产精品| 美女视频黄久久| 日韩天堂在线| 亚洲黄色中文字幕| 极品日韩av| 噜噜噜躁狠狠躁狠狠精品视频| 免费在线观看不卡| 久久精品99国产精品日本| 国产欧美一区二区精品久久久 | 国产精品香蕉| 欧美在线资源| 日韩一区精品字幕| 免费视频久久| 视频一区二区三区在线| 综合激情网站| 激情综合五月| 精品久久97| 欧美另类中文字幕| 国产情侣一区| 婷婷精品在线观看| 国产欧美日韩视频在线| 国产模特精品视频久久久久| 99国产精品99久久久久久粉嫩| 日韩精品午夜视频| 捆绑调教日本一区二区三区| 午夜精品成人av| 亚洲三区欧美一区国产二区| 国产精品色在线网站| 免费观看亚洲| 天堂av在线一区| 日韩精品一区二区三区av| 久久免费影院| 国产一区二区高清| 国产精品66| 久久婷婷av| 爽好多水快深点欧美视频| 免费在线亚洲欧美| 亚洲高清av| 日韩精彩视频在线观看| 波多视频一区| 综合欧美精品| 91日韩欧美| 综合亚洲自拍| 日韩理论视频| 日本不卡一区二区| 黄色网一区二区| 亚洲一区日韩| 国产精品嫩草影院在线看| 久久激情中文| 国产伦理久久久久久妇女| 国产尤物精品| 日韩二区三区四区| 伊人精品一区| 精品中文字幕一区二区三区四区| 亚洲欧美网站| 精品国产中文字幕第一页| 视频一区二区国产|