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

您的位置:首頁技術(shù)文章
文章詳情頁

python中pathlib模塊的基本用法與總結(jié)

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

前言

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

pathlib 是Python內(nèi)置庫,Python 文檔給它的定義是:The pathlib module ? object-oriented filesystem paths(面向?qū)ο蟮奈募到y(tǒng)路徑)。pathlib 提供表示文件系統(tǒng)路徑的類,其語義適用于不同的操作系統(tǒng)。

python中pathlib模塊的基本用法與總結(jié)

更多詳細的內(nèi)容可以參考官方文檔: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) # 將路徑通過分隔符分割成一個元組

運行結(jié)果如下:

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())

運行結(jié)果如下:

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) # 文件的字節(jié)大小print(p.stat().st_ctime) # 文件創(chuàng)建時間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’該文件創(chuàng)建時間:{creat_time}’)print(f’上次修改該文件的時間:{st_mtime}’)

運行結(jié)果如下:

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該文件創(chuàng)建時間:2020-08-13 20:09:45.765748上次修改該文件的時間:2020-08-14 09:00:26.971164

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

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())

運行結(jié)果如下:

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)

運行結(jié)果如下:

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模塊的基本用法與總結(jié)

總結(jié)

到此這篇關(guān)于python中pathlib模塊的基本用法與總結(jié)的文章就介紹到這了,更多相關(guān)python pathlib模塊用法內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標簽: Python 編程
相關(guān)文章:
日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区
在线精品福利| 日韩高清成人| 国产精品免费看| 国产一区二区中文| 精品日韩视频| sm久久捆绑调教精品一区| 亚洲精品少妇| 亚洲精品动态| 7777精品| 国产无遮挡裸体免费久久| 日本午夜精品一区二区三区电影 | 成人精品中文字幕| 日韩欧美精品| 欧美日韩高清| 免费国产自线拍一欧美视频| 99综合视频| 一区二区三区四区在线观看国产日韩| 噜噜噜久久亚洲精品国产品小说| 午夜电影亚洲| 麻豆9191精品国产| 亚洲精品影视| 国产精品欧美一区二区三区不卡| 麻豆成人综合网| 国产一区二区三区黄网站| 成人综合一区| 欧美/亚洲一区| 蜜臀va亚洲va欧美va天堂| 日韩超碰人人爽人人做人人添| 欧美日韩国产一区二区在线观看| 国产精品一区二区中文字幕| 老司机精品视频在线播放| 欧美国产小视频| 136国产福利精品导航网址| 国产精品毛片| 日韩精选在线| 久久91视频| 亚洲欧美伊人| 88久久精品| 日韩欧美一区免费| 亚洲综合丁香| 国产剧情在线观看一区| 国产福利片在线观看| 黄色国产精品| 国产欧美自拍| 亚洲成人精品| 青青草91视频| 日韩一区二区三区在线免费观看| 久久国产66| 老司机免费视频一区二区| 日本免费久久| 视频在线在亚洲| 久久不见久久见国语| 久久久久91| 日韩高清三区| 久久美女性网| 欧美亚洲tv| 五月天激情综合网| 国产精品亚洲综合在线观看| 国产一区不卡| 老牛影视一区二区三区| 你懂的网址国产 欧美| 久久中文字幕二区| 97成人超碰| 国精品一区二区三区| 日韩欧美中文在线观看| 97se综合| 日韩av资源网| 国内精品99| 久久精品天堂| 一区二区三区国产在线| 日韩欧美精品| 国产日本精品| 久久亚洲欧洲| 日韩成人亚洲| 久久超碰99| 亚洲三级国产| 99久久夜色精品国产亚洲狼| 国产精品网址| 丝袜美腿一区二区三区| 福利片在线一区二区| 日韩中文字幕| 红桃视频国产精品| 欧美天堂视频| 国产精品66| 偷拍亚洲精品| 欧美午夜不卡| 久久精品官网| 国产91在线播放精品| 国产精品二区影院| 亚洲ww精品| 亚洲一区网站| 国产一区清纯| 日本蜜桃在线观看视频| 精品国产欧美日韩| 久久国产视频网| 久久国产精品亚洲77777| 99久久婷婷| 日韩一区二区三区免费播放| 久久一区欧美| 国产精品第一| 97精品资源在线观看| 石原莉奈在线亚洲二区| 国产综合精品| 久久精品中文| 日韩毛片视频| av免费不卡国产观看| 国产激情综合| 69堂免费精品视频在线播放| 亚洲欧美日本国产| 美国三级日本三级久久99 | 久久av免费| 日韩av中文字幕一区| 亚洲精品在线a| 亚洲人妖在线| 蜜桃久久久久久| 伊人国产精品| 亚洲精品麻豆| 亚洲精品一二三**| 亚洲人成网77777色在线播放| 妖精视频成人观看www| 91九色精品国产一区二区| 四虎884aa成人精品最新| 日韩一区亚洲二区| 国产精品99视频| 岛国av免费在线观看| 欧美日韩国产观看视频| 欧美国产小视频| 日韩三区免费| 激情久久婷婷| 伊人久久亚洲美女图片| 国产日韩综合| 蜜桃一区二区三区在线| 亚洲综合激情在线| 午夜电影一区| 日本欧美一区二区在线观看| 日韩va欧美va亚洲va久久| 清纯唯美亚洲综合一区| 国产日本精品| 国产精品超碰| 精品国产午夜| 日韩在线第七页| 五月天综合网站| 亚洲一区二区三区中文字幕在线观看| 欧美日韩中文| 91精品一区国产高清在线gif | 免费精品国产| 欧美日韩国产免费观看视频| 性色av一区二区怡红| 日韩**一区毛片| 蜜桃精品视频| 成人国产综合| 久久五月天小说| 久久成人亚洲| 国产麻豆精品| 亚洲精品成人图区| 91成人精品| 日本精品另类| 欧美黄页在线免费观看| 日韩免费在线| 中文不卡在线| 国产精品久久久久毛片大屁完整版| 精品亚洲成人| 香蕉久久99| 日韩在线观看中文字幕| 麻豆国产欧美日韩综合精品二区| 国产一二在线播放| 亚洲一区激情| 国产精品www.| 欧美日韩一区二区综合| 首页亚洲欧美制服丝腿| 国产精品theporn| 久久国产主播| 亚洲区欧美区| 精品国产欧美| 中文久久精品| 国产精品久久亚洲不卡| 亚洲一级特黄| 国产精品入口久久| 免费观看久久av| 国产图片一区| 99久久激情| 久久国产欧美日韩精品| 国产精品久久久久av电视剧| 免费在线成人网| 美女毛片一区二区三区四区最新中文字幕亚洲 | 91精品国产自产观看在线| 国产成人精品999在线观看| 国产精品婷婷| 精品一区二区男人吃奶 | 日韩大片在线观看| 热久久国产精品| 国产精品久久久久久模特| 欧美特黄一区| 成人黄色av| 亚洲精品激情| 久久精品影视| 国产精品一区二区av日韩在线| 欧美日韩高清| 美女视频网站久久| 免费成人在线观看|