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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

new file https 找不到路径_Python3用pathlib模块替代os.path进行文件路径的操作

發布時間:2024/9/30 python 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 new file https 找不到路径_Python3用pathlib模块替代os.path进行文件路径的操作 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

本文使用 Zhihu On VSCode 創作并發布

在 Python 3.4 之前和路徑相關操作函數都放在 os 模塊里面,尤其是os.path這個子模塊,可以說os.path模塊非常常用。而在 Python 3.4,標準庫添加了新的模塊 - pathlib,它使用面向對象的編程方式來表示文件系統路徑。

1.Path.cwd()和Path.home()

Path.cwd() 返回一個新的表示當前目錄的路徑對象(和 os.getcwd() 返回的相同)

Path.home()返回一個表示當前用戶家目錄的新路徑對象(和 os.path.expanduser() 構造含 ~ 路徑返回的相同)

#!/usr/bin/env python3from pathlib import Pathprint(f"Current directory: {Path.cwd()}") print(f"Home directory: {Path.home()}")output:Current directory: h:Python_LearnPython3_Test Home directory: C:Usersliming

2.Path.mkdir()

使用mkdir()創建一個新目錄。

#!/usr/bin/env python3from pathlib import Pathpath = Path.cwd() / 'new'path.mkdir()

該示例在當前工作目錄內創建一個new目錄。

3.連接路徑

路徑可以用/運算符或joinpath()方法連接。

#!/usr/bin/env python3from pathlib import Pathpath = Path.home()docs = path / 'Documents' pictures = path / 'Pictures'print(docs) print(pictures)output:C:UserslimingDocuments C:UserslimingPictures

4.Path.touch()

touch()創建一個新的空文件;它等效于Linux中的touch 命令。

#!/usr/bin/env python3from pathlib import PathPath('simple.txt').touch()

我們創建一個新的空simple.txt。

5.Path.rename()

rename()重命名文件或目錄。

#!/usr/bin/env python3from pathlib import Pathpath = Path('names.txt')path.rename('mynames.txt')

該示例將當前工作目錄中的names.txt重命名為mynames.txt。

6.Path.parent()

使用parent()和parents(),我們可以獲得路徑的邏輯父級。

#!/usr/bin/env python3from pathlib import Pathpath = Path('C:/Users/liming/Documents')print(f"The parent directory of {path} is {path.parent}") print(f"The parent of the parent of {path} is {path.parent.parent}")print(f"All the parents of {path.parent}: ")print(list(path.parents))output:The parent directory of C:UserslimingDocuments is C:Usersliming The parent of the parent of C:UserslimingDocuments is C:Users All the parents of C:Usersliming: [WindowsPath('C:/Users/liming'), WindowsPath('C:/Users'), WindowsPath('C:/')]

7.Path.iterdir()

當路徑指向一個目錄時,產生該路徑下的對象的路徑:

#!/usr/bin/env python3from pathlib import Pathpath = Path('C:/Users/Jano/Documents')dirs = [file for file in path.iterdir() if file.is_dir()] print(dirs)output:[WindowsPath('C:/Users/liming/Documents/MobaXterm'), WindowsPath('C:/Users/liming/Documents/My Music'), WindowsPath('C:/Users/liming/Documents/My Pictures'), WindowsPath('C:/Users/liming/Documents/My Videos'), WindowsPath('C:/Users/liming/Documents/NetSarang Computer'), WindowsPath('C:/Users/liming/Documents/OneNote 筆記本'), WindowsPath('C:/Users/liming/Documents/Tencent Files'), WindowsPath('C:/Users/liming/Documents/WeChat Files'), WindowsPath('C:/Users/liming/Documents/自定義 Office 模板')]

該示例打印指定目錄的子目錄。 我們檢查路徑對象是否為is_dir()目錄。

以下示例在指定目錄內打印文件。

#!/usr/bin/env python3from pathlib import Pathpath = Path('C:/Users/Jano/Downloads')files = [file for file in path.iterdir() if file.is_file()] print(files)output:[WindowsPath('C:/Users/liming/Downloads/desktop.ini'), WindowsPath('C:/Users/liming/Downloads/kanokari_anime-1206182114162593792(20191215_200000)-1286696392266854400(20200725_001500)-media.zip'), WindowsPath('C:/Users/liming/Downloads/kanokari_anime-1286705759783272449(20200725_005213)-1311954079158022144(20201002_170002)-media.zip'), WindowsPath('C:/Users/liming/Downloads/MobaXterm_Installer_v20.3.zip'), WindowsPath('C:/Users/liming/Downloads/mobiles.apk'), WindowsPath('C:/Users/liming/Downloads/v2-bbe3d5f7adcd7c3caac7fab0580cde77.png')]

8.路徑遍歷

球形模式使用通配符指定文件名集。 例如,*.txt表示所有名稱以.txt結尾的文件。 *是代表任何字符串的通配符。 另一個常見的通配符是問號(?),代表一個字符。

路徑提供glob()和rglob()。 后者用于遞歸 glob。 它將**/添加到給定模式的前面。

#!/usr/bin/env python3from pathlib import Pathpath = Path('C:/Users/liming/Documents/python')for file in path.rglob('*.py'):print(file)for file in path.glob('**/*.py'):print(file)# 這兩個操作是等效的。

該示例打印指定目錄及其所有子目錄中的所有Python文件。 請注意,此類操作可能非常耗時。

9.Path.open(()

打開路徑指向的文件,就像內置的 open() 函數所做的一樣:

#!/usr/bin/env python3from pathlib import Pathpath = Path('myfile.txt') with path.open() as f:line=f.readline()print(line)output:hello world!

10.Path.exists()

此路徑是否指向一個已存在的文件或目錄:

#!/usr/bin/env python3from pathlib import Pathpath = Path('myfile.txt') print(f"{path} file is exist {path.exists()}")path=Path("hello.py") print(f"{path} file is exist {path.exists()}")output:myfile.txt file is exist True hello.py file is exist False

總結

對應的 os 模塊的工具

以上就是pathlib中一些常用的方法并且和os.path中的方法想對應。至于其他方法,請參考python官方文檔。

參考鏈接

  • https://docs.python.org/zh-cn/3.7/library/pathlib.html
與50位技術專家面對面20年技術見證,附贈技術全景圖

總結

以上是生活随笔為你收集整理的new file https 找不到路径_Python3用pathlib模块替代os.path进行文件路径的操作的全部內容,希望文章能夠幫你解決所遇到的問題。

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