{ "cells": [ { "cell_type": "markdown", "id": "b14a30b1-de3b-4452-a92b-fba44c3242ea", "metadata": {}, "source": [ "# pathlib深入浅出" ] }, { "cell_type": "markdown", "id": "c2267420", "metadata": {}, "source": [ "最近在做图像相关的算法,经常有对文件路径的一些条件遍历操作,发现最好使的还是下面这行神奇代码。" ] }, { "cell_type": "markdown", "id": "8d46788d", "metadata": {}, "source": [ "```python\n", "from pathlib import Path\n", "imgs = [str(x) for x in Path('./dataset/images/').rglob('*.jp*g') if 'checkpoint' not in str(x)]\n", "```" ] }, { "cell_type": "markdown", "id": "feccb3fd-fdfb-480e-b722-11d7890a6207", "metadata": {}, "source": [ "相比于传统的os模块的函数式文件操作,使用pathlib模块来操作文件系统更加简单,可读性更强。\n", "\n", "pathlib库的核心类是pathlib.Path,所有功能都可以通过调用这个类的属性和方法进行实现,非常的面向对象。\n" ] }, { "cell_type": "markdown", "id": "4546d0b0-5c5a-45d5-8df7-f7d7fb2cadda", "metadata": {}, "source": [ "## 一,Path构造" ] }, { "cell_type": "code", "execution_count": 1, "id": "00c902ea-6411-45dc-8ea8-7c3302fadc25", "metadata": {}, "outputs": [], "source": [ "from pathlib import Path " ] }, { "cell_type": "code", "execution_count": 16, "id": "33357360-7e5b-4bd4-b987-ae52fc28cc6b", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "../demo.py\n" ] } ], "source": [ "#指定路径\n", "mypath = Path('../demo.py')\n", "print(mypath)" ] }, { "cell_type": "code", "execution_count": 11, "id": "eb917752-e44e-47b4-98f2-33eb1b7b7af0", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "/Users/liangyun2/CodeFiles/machine_learning_AK47/1_Feature_Engineering\n" ] } ], "source": [ "#当前路径\n", "cwd = Path.cwd() \n", "print(str(cwd))" ] }, { "cell_type": "code", "execution_count": 7, "id": "842b875d-1481-45d3-911b-c18e53ac5e9c", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "/Users/liangyun2\n" ] } ], "source": [ "#home路径\n", "home = Path.home() \n", "print(str(home))" ] }, { "cell_type": "code", "execution_count": null, "id": "f8d02f0e-26f4-4395-b756-fb0fb0943854", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "b6e05cde-fda0-4659-857a-66666a0afda2", "metadata": {}, "source": [ "## 二,Path属性" ] }, { "cell_type": "code", "execution_count": 19, "id": "61d249f6-c9f9-4e7c-9e65-7a643fbb9b73", "metadata": {}, "outputs": [], "source": [ "mypath = Path.cwd()" ] }, { "cell_type": "code", "execution_count": 21, "id": "083d7241-8056-4889-a23b-c2de4277a63d", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'1_Feature_Engineering'" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mypath.name #文件名" ] }, { "cell_type": "code", "execution_count": 20, "id": "64bfecdf-d259-451b-9694-5ec313c5a587", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "''" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mypath.suffix #后缀" ] }, { "cell_type": "code", "execution_count": 22, "id": "a0449716-d876-41ec-b399-2a3ee6b1f151", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "PosixPath('/Users/liangyun2/CodeFiles/machine_learning_AK47')" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mypath.parent #" ] }, { "cell_type": "code", "execution_count": 24, "id": "bc9dac61-a8c2-4ef1-a9fa-5ab9e19b1b94", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[PosixPath('/Users/liangyun2/CodeFiles/machine_learning_AK47'),\n", " PosixPath('/Users/liangyun2/CodeFiles'),\n", " PosixPath('/Users/liangyun2'),\n", " PosixPath('/Users'),\n", " PosixPath('/')]" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list(mypath.parents)" ] }, { "cell_type": "code", "execution_count": null, "id": "d80794d2-ebf3-4842-b711-3d2f807db5ed", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "a1d6ba15-1e6b-44cc-aefa-8c0c9ce61935", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "ef64de0e-6a49-4f01-91a7-6a058e894aba", "metadata": {}, "source": [ "## 三,Path遍历" ] }, { "cell_type": "code", "execution_count": 46, "id": "5e564002-0105-42d9-bf2f-e4dfc6b33d46", "metadata": {}, "outputs": [], "source": [ "mypath = Path.cwd().parent " ] }, { "cell_type": "code", "execution_count": 47, "id": "2eee142b-7b9f-4589-9ea6-41f0985bf24c", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "/Users/liangyun2/CodeFiles/machine_learning_AK47/1_Feature_Engineering/demo.py\n" ] } ], "source": [ "file = mypath/'1_Feature_Engineering'/'demo.py'\n", "print(file)" ] }, { "cell_type": "code", "execution_count": 48, "id": "bdaaabd5-d56b-49e8-8ab5-c4d426e60b50", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 48, "metadata": {}, "output_type": "execute_result" } ], "source": [ "file.exists()" ] }, { "cell_type": "code", "execution_count": 49, "id": "e49fc26b-9805-45f6-bf39-5397186cdc8e", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "/Users/liangyun2/CodeFiles/machine_learning_AK47/.DS_Store\n", "/Users/liangyun2/CodeFiles/machine_learning_AK47/3_Tune_Tools\n", "/Users/liangyun2/CodeFiles/machine_learning_AK47/6_CV\n", "/Users/liangyun2/CodeFiles/machine_learning_AK47/8_RL\n", "/Users/liangyun2/CodeFiles/machine_learning_AK47/README.ipynb\n", "/Users/liangyun2/CodeFiles/machine_learning_AK47/utils\n", "/Users/liangyun2/CodeFiles/machine_learning_AK47/models\n", "/Users/liangyun2/CodeFiles/machine_learning_AK47/2_Base_Models\n", "/Users/liangyun2/CodeFiles/machine_learning_AK47/push_to_github.ipynb\n", "/Users/liangyun2/CodeFiles/machine_learning_AK47/.gitignore\n", "/Users/liangyun2/CodeFiles/machine_learning_AK47/.ipynb_checkpoints\n", "/Users/liangyun2/CodeFiles/machine_learning_AK47/1_Feature_Engineering\n", "/Users/liangyun2/CodeFiles/machine_learning_AK47/.git\n", "/Users/liangyun2/CodeFiles/machine_learning_AK47/data\n", "/Users/liangyun2/CodeFiles/machine_learning_AK47/7_NLP\n", "/Users/liangyun2/CodeFiles/machine_learning_AK47/5_Integrated_Examples\n", "/Users/liangyun2/CodeFiles/machine_learning_AK47/4_Ensemble_Tools\n" ] } ], "source": [ "for sub in mypath.iterdir():\n", " print(sub)" ] }, { "cell_type": "code", "execution_count": 52, "id": "6ff66066-4f29-45bd-8ac9-3cb8e94c4918", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "/Users/liangyun2/CodeFiles/machine_learning_AK47/.DS_Store\n", "/Users/liangyun2/CodeFiles/machine_learning_AK47/3_Tune_Tools\n", "/Users/liangyun2/CodeFiles/machine_learning_AK47/6_CV\n", "/Users/liangyun2/CodeFiles/machine_learning_AK47/8_RL\n", "/Users/liangyun2/CodeFiles/machine_learning_AK47/README.ipynb\n", "/Users/liangyun2/CodeFiles/machine_learning_AK47/utils\n", "/Users/liangyun2/CodeFiles/machine_learning_AK47/models\n", "/Users/liangyun2/CodeFiles/machine_learning_AK47/2_Base_Models\n", "/Users/liangyun2/CodeFiles/machine_learning_AK47/push_to_github.ipynb\n", "/Users/liangyun2/CodeFiles/machine_learning_AK47/.gitignore\n", "/Users/liangyun2/CodeFiles/machine_learning_AK47/.ipynb_checkpoints\n", "/Users/liangyun2/CodeFiles/machine_learning_AK47/1_Feature_Engineering\n", "/Users/liangyun2/CodeFiles/machine_learning_AK47/.git\n", "/Users/liangyun2/CodeFiles/machine_learning_AK47/data\n", "/Users/liangyun2/CodeFiles/machine_learning_AK47/7_NLP\n", "/Users/liangyun2/CodeFiles/machine_learning_AK47/5_Integrated_Examples\n", "/Users/liangyun2/CodeFiles/machine_learning_AK47/4_Ensemble_Tools\n" ] } ], "source": [ "for sub in mypath.glob('*'):\n", " print(sub)" ] }, { "cell_type": "code", "execution_count": 53, "id": "1de87378-fbd5-4696-9880-860333033c5a", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "/Users/liangyun2/CodeFiles/machine_learning_AK47/README.ipynb\n", "/Users/liangyun2/CodeFiles/machine_learning_AK47/push_to_github.ipynb\n" ] } ], "source": [ "for sub in mypath.glob('*.ipynb'):\n", " print(sub)" ] }, { "cell_type": "code", "execution_count": 56, "id": "bd384a42-ad8e-451f-a273-bb17926fb931", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "9670" ] }, "execution_count": 56, "metadata": {}, "output_type": "execute_result" } ], "source": [ "files = [x for x in mypath.rglob('*') if x.is_file()]\n", "len(files)" ] }, { "cell_type": "code", "execution_count": null, "id": "82e619bf-d7cd-43f1-9f0a-59d43815d978", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "aea6a885-383e-4981-84c8-5541b7890447", "metadata": {}, "source": [ "## 四,Path操作" ] }, { "cell_type": "code", "execution_count": 107, "id": "10642a22-ffe8-48c3-9223-4e39fcff1762", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'hello world!\\n你好,中国!\\n'" ] }, "execution_count": 107, "metadata": {}, "output_type": "execute_result" } ], "source": [ "path = Path.cwd()/'test.txt'\n", "\n", "if not path.exists():\n", " path.touch() #创建文件\n", " \n", "#读写内容\n", "path.write_text(\"hello world!\\n你好,中国!\\n\")\n", "path.read_text()\n" ] }, { "cell_type": "code", "execution_count": 108, "id": "8809b991-1b6a-4471-9028-1c6cab2ce4d0", "metadata": {}, "outputs": [], "source": [ "#增量形式写\n", "with path.open('a') as f:\n", " f.write('你好,北京')" ] }, { "cell_type": "code", "execution_count": 109, "id": "d67f9b39-d829-44a1-914e-53b37eb7e854", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'hello world!\\n你好,中国!\\n你好,北京'" ] }, "execution_count": 109, "metadata": {}, "output_type": "execute_result" } ], "source": [ "path.read_text()" ] }, { "cell_type": "code", "execution_count": 113, "id": "c97ccf77-312b-4d66-a66d-4244dffa5578", "metadata": {}, "outputs": [], "source": [ "#重命名\n", "path = path.rename('hello.txt')" ] }, { "cell_type": "code", "execution_count": 112, "id": "b9fe8c59-fb7a-4a3e-8470-b4013316b1fb", "metadata": {}, "outputs": [], "source": [ "# %load hello.txt\n", "hello world!\n", "你好,中国!\n", "你好,北京" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.0" } }, "nbformat": 4, "nbformat_minor": 5 }