{"id":1437,"date":"2022-07-11T18:55:06","date_gmt":"2022-07-11T18:55:06","guid":{"rendered":"http:\/\/feellikelearning.com\/?p=1437"},"modified":"2022-12-10T07:35:24","modified_gmt":"2022-12-10T07:35:24","slug":"python-pathlib-survival-guide-a-few-examples-to-learn","status":"publish","type":"post","link":"https:\/\/feellikelearning.com\/index.php\/2022\/07\/11\/python-pathlib-survival-guide-a-few-examples-to-learn\/","title":{"rendered":"Python Pathlib\u751f\u5b58\u624b\u518c\uff0c\u51e0\u4e2a\u77ed\u4f8b\u5b50\u6781\u901f\u4e0a\u624b"},"content":{"rendered":"\n<p>Pathlib\u662fPython\u91cc\u81ea\u5e26\u7684\u5904\u7406OS\u8def\u5f84\u7684library\uff0c\u529f\u80fd\u975e\u5e38\u5f3a\u5927\uff0c\u53ef\u4ee5\u4ee3\u66ff\u539f\u6765\u7684<code data-enlighter-language=\"python\" class=\"EnlighterJSRAW\">os.path<\/code>\u3002\u672c\u6587\u63d0\u4f9b\u51e0\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\u4f9b\u67e5\u8be2\uff0c\u5e2e\u60a8\u6781\u901f\u4e0a\u624b\uff0c\u5f15\u7528\u5230\u81ea\u5df1\u7684python project\u91cc\u3002<\/p>\n\n\n\n<h2>\u521b\u5efa\u64cd\u4f5cPath\u5bf9\u8c61\u4f8b\u5b50<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code><code data-enlighter-language=\"python\" class=\"EnlighterJSRAW\">p = Path('\/home\/feellikelearning')<\/code><\/code><\/pre>\n\n\n\n<p>\u53ef\u4ee5\u7528\u201d\/\u201c\u76f4\u63a5\u5f97\u5230\u4e0b\u4e00\u5c42\u76ee\u5f55\u6216\u6587\u4ef6\u7684Path\u5bf9\u8c61<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code data-enlighter-language=\"python\" class=\"EnlighterJSRAW\">codeDir = p \/ 'code'<\/code><\/code><\/pre>\n\n\n\n<h2>Home Path\u5bf9\u8c61\u4f8b\u5b50<\/h2>\n\n\n\n<p>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0cPath constructor\u7684\u53c2\u6570\u4e0d\u8ba4\u8bc6~\u3002\u9700\u8981\u7528<code data-enlighter-language=\"python\" class=\"EnlighterJSRAW\">Path.home()<\/code>\u3002<\/p>\n\n\n\n<p>\u9a8c\u8bc1tilda\u4e0dwork<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code data-enlighter-language=\"python\" class=\"EnlighterJSRAW\">p = Path('~')\nprint(p.as_posix())\nprint((p \/ 'code').as_posix())\nprint((p \/ 'code').exists()) # False<\/code><\/code><\/pre>\n\n\n\n<p>\u9a8c\u8bc1Path.home() work<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code data-enlighter-language=\"python\" class=\"EnlighterJSRAW\">p = Path.home()\nprint(p.as_posix())\nprint((p \/ 'code').as_posix())\nprint((p \/ 'code').exists()) # True<\/code><\/code><\/pre>\n\n\n\n<h2>\u5f53\u524d\u76ee\u5f55\u4f8b\u5b50<\/h2>\n\n\n\n<p>\u5982\u679c\u9700\u8981\u5f53\u524d\u76ee\u5f55\u7684\u5bf9\u8c61\u53ef\u4ee5\u7528<code data-enlighter-language=\"python\" class=\"EnlighterJSRAW\">cwd<\/code>\u51fd\u6570\u6216\u8005<code data-enlighter-language=\"python\" class=\"EnlighterJSRAW\">.<\/code>\u52a0<code data-enlighter-language=\"python\" class=\"EnlighterJSRAW\">resolve<\/code>\u51fd\u6570<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code data-enlighter-language=\"python\" class=\"EnlighterJSRAW\">p = Path.cwd()\np = Path('.').resolve()<\/code><\/code><\/pre>\n\n\n\n<h2>\u904d\u5386\u6587\u4ef6\u5939\u4f8b\u5b50<\/h2>\n\n\n\n<p>\u4e5f\u5c31\u662fimplement bash\u547d\u4ee4<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code data-enlighter-language=\"bash\" class=\"EnlighterJSRAW\">find .<\/code><\/code><\/pre>\n\n\n\n<p>\u7528pathlib\u5b9e\u73b0\u5982\u4e0b<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">from pathlib import Path\n\ndef python_find(target_path_str):\n    def _python_find(p):\n        itr = p.iterdir()\n        for f in itr:\n            print(f.as_posix())\n            if f.is_dir():\n                _python_find(f)\n\n    p = pathlib.Path(target_path_str)\n    _python_find(p)\n\npython_find('some\/path')<\/pre>\n\n\n\n<p>\u5916\u9762\u51fd\u6570python_find\u63a5\u53d7path string\uff0c\u91cc\u9762\u7684\u9012\u5f52\u51fd\u6570_python_find\u63a5\u53d7pathlib.Path object\u3002\u7528iterdir\u5217\u51fa\u5f53\u524dinput path\u4e0b\u7684\u6240\u6709\u5bf9\u8c61\uff0c\u5148\u6253\u5370\u5b83\u4eec\u7684\u8def\u5f84\uff0c\u5982\u679c\u662f\u76ee\u5f55\uff0c\u5c31\u7ee7\u7eed\u8c03\u7528\u9012\u5f52\u51fd\u6570_python_find\u6253\u5370\u4e0b\u4e00\u5c42\u76ee\u5f55\u91cc\u9762\u7684\u5185\u5bb9\u3002<\/p>\n\n\n\n<h2>Pathlib\u5220\u9664\u6587\u4ef6\u4f8b\u5b50<\/h2>\n\n\n\n<h3>\u5220\u9664\u6587\u4ef6<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code><code data-enlighter-language=\"python\" class=\"EnlighterJSRAW\">p = Path('\/some\/filepath\/todelete')\np.unlink()<\/code><\/code><\/pre>\n\n\n\n<h3>\u5220\u9664\u76ee\u5f55\/\u6587\u4ef6\u5939<\/h3>\n\n\n\n<p>rmdir\u53ea\u80fd\u5220\u9664\u7a7a\u7684\u6587\u4ef6\u5939\/\u76ee\u5f55\uff0c\u5982\u679c\u5185\u90e8\u8fd8\u6709\u5176\u4ed6\u6587\u4ef6\u6216\u76ee\u5f55\u4f1a\u62a5\u9519\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code data-enlighter-language=\"python\" class=\"EnlighterJSRAW\">p = Path('\/some\/dir\/todelete')\np.rmdir()<\/code><\/code><\/pre>\n\n\n\n<h3>\u5220\u9664\u975e\u7a7a\u76ee\u5f55\/\u6587\u4ef6\u5939<\/h3>\n\n\n\n<p>\u8fd9\u4e2a\u53ea\u80fd\u81ea\u5df1\u7528\u4e0a\u9762\u4e24\u4e2a\u51fd\u6570\u7528\u9012\u5f52\u5b9e\u73b0\u3002\u8fd9\u91cc\u6211\u589e\u52a0\u4e86\u663e\u793a\u88ab\u5220\u9664\u6587\u4ef6size\u7684\u4fe1\u606f\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">def rmdirRecursive(somePath: pathlib.PosixPath):\n    for f in somePath.iterdir():\n        if f.is_file():\n            filesize = f.stat().st_size\n            print(f'deleting {f.as_posix()} -> {filesize}')\n            f.unlink()\n        elif f.is_dir():\n            rmdirRecursive(f)\n\n    print(f'deleting directory {somePath.as_posix()}')\n    somePath.rmdir()\n<\/pre>\n\n\n\n<h2>\u53c2\u8003<\/h2>\n\n\n\n<ul><li><a href=\"https:\/\/yifei.me\/note\/2705\">\u4e00\u5206\u949f\u5b66\u4e00\u4e2a Python \u6807\u51c6\u5e93\u4e4b Pathlib<\/a><\/li><li><a href=\"https:\/\/stackoverflow.com\/questions\/50186904\/pathlib-recursively-remove-directory\">PathLib recursively remove directory?<\/a><\/li><li><a href=\"https:\/\/docs.python.org\/3\/library\/pathlib.html\"><code>pathlib<\/code>&nbsp;\u2014 Object-oriented filesystem paths<\/a><\/li><\/ul>\n\n\n\n<p><a href=\"http:\/\/feellikelearning.com\/index.php\/2022\/07\/11\/python-pathlib-survival-guide-a-few-examples-to-learn\/\" target=\"_blank\" rel=\"noreferrer noopener\">\u672c\u6587\u94fe\u63a5<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Pathlib\u662fPython\u91cc\u81ea\u5e26\u7684\u5904\u7406OS\u8def\u5f84\u7684library\uff0c\u529f\u80fd\u975e\u5e38\u5f3a\u5927\uff0c\u53ef\u4ee5\u4ee3\u66ff\u539f\u6765\u7684os.path\u3002\u672c\u6587\u63d0\u4f9b\u51e0\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\u4f9b\u67e5\u8be2\uff0c\u5e2e\u60a8\u6781\u901f\u4e0a\u624b\uff0c\u5f15\u7528\u5230\u81ea\u5df1\u7684python project\u91cc\u3002 \u521b\u5efa\u64cd\u4f5cPath\u5bf9\u8c61\u4f8b\u5b50 \u53ef\u4ee5\u7528\u201d\/\u201c\u76f4\u63a5\u5f97\u5230\u4e0b\u4e00\u5c42\u76ee\u5f55\u6216\u6587\u4ef6\u7684Path\u5bf9\u8c61 Home Path\u5bf9\u8c61\u4f8b\u5b50 \u9700\u8981\u6ce8\u610f\u7684\u662f\uff0cPath constructor\u7684\u53c2\u6570\u4e0d\u8ba4\u8bc6~\u3002\u9700\u8981\u7528Path.home()\u3002 \u9a8c\u8bc1tilda\u4e0dwork \u9a8c\u8bc1Path.home() work \u5f53\u524d\u76ee\u5f55\u4f8b\u5b50 \u5982\u679c\u9700\u8981\u5f53\u524d\u76ee\u5f55\u7684\u5bf9\u8c61\u53ef\u4ee5\u7528cwd\u51fd\u6570\u6216\u8005.\u52a0resolve\u51fd\u6570 \u904d\u5386\u6587\u4ef6\u5939\u4f8b\u5b50 \u4e5f\u5c31\u662fimplement bash\u547d\u4ee4 \u7528pathlib\u5b9e\u73b0\u5982\u4e0b \u5916\u9762\u51fd\u6570python_find\u63a5\u53d7path string\uff0c\u91cc\u9762\u7684\u9012\u5f52\u51fd\u6570_python_find\u63a5\u53d7pathlib.Path object\u3002\u7528iterdir\u5217\u51fa\u5f53\u524dinput path\u4e0b\u7684\u6240\u6709\u5bf9\u8c61\uff0c\u5148\u6253\u5370\u5b83\u4eec\u7684\u8def\u5f84\uff0c\u5982\u679c\u662f\u76ee\u5f55\uff0c\u5c31\u7ee7\u7eed\u8c03\u7528\u9012\u5f52\u51fd\u6570_python_find\u6253\u5370\u4e0b\u4e00\u5c42\u76ee\u5f55\u91cc\u9762\u7684\u5185\u5bb9\u3002 Pathlib\u5220\u9664\u6587\u4ef6\u4f8b\u5b50 \u5220\u9664\u6587\u4ef6 \u5220\u9664\u76ee\u5f55\/\u6587\u4ef6\u5939 rmdir\u53ea\u80fd\u5220\u9664\u7a7a\u7684\u6587\u4ef6\u5939\/\u76ee\u5f55\uff0c\u5982\u679c\u5185\u90e8\u8fd8\u6709\u5176\u4ed6\u6587\u4ef6\u6216\u76ee\u5f55\u4f1a\u62a5\u9519\u3002 \u5220\u9664\u975e\u7a7a\u76ee\u5f55\/\u6587\u4ef6\u5939 \u8fd9\u4e2a\u53ea\u80fd\u81ea\u5df1\u7528\u4e0a\u9762\u4e24\u4e2a\u51fd\u6570\u7528\u9012\u5f52\u5b9e\u73b0\u3002\u8fd9\u91cc\u6211\u589e\u52a0\u4e86\u663e\u793a\u88ab\u5220\u9664\u6587\u4ef6size\u7684\u4fe1\u606f\u3002 \u53c2\u8003 \u4e00\u5206\u949f\u5b66\u4e00\u4e2a Python \u6807\u51c6\u5e93\u4e4b Pathlib PathLib recursively remove directory? pathlib&nbsp;\u2014 Object-oriented filesystem paths&#8230;<br \/><a class=\"read-more-button\" href=\"https:\/\/feellikelearning.com\/index.php\/2022\/07\/11\/python-pathlib-survival-guide-a-few-examples-to-learn\/\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false},"categories":[10,3],"tags":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v19.10 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Python Pathlib\u751f\u5b58\u624b\u518c\uff0c\u51e0\u4e2a\u77ed\u4f8b\u5b50\u6781\u901f\u4e0a\u624b | Feel Like Learning<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/feellikelearning.com\/index.php\/2022\/07\/11\/python-pathlib-survival-guide-a-few-examples-to-learn\/?variant=zh-hant\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Pathlib\u751f\u5b58\u624b\u518c\uff0c\u51e0\u4e2a\u77ed\u4f8b\u5b50\u6781\u901f\u4e0a\u624b | Feel Like Learning\" \/>\n<meta property=\"og:description\" content=\"Pathlib\u662fPython\u91cc\u81ea\u5e26\u7684\u5904\u7406OS\u8def\u5f84\u7684library\uff0c\u529f\u80fd\u975e\u5e38\u5f3a\u5927\uff0c\u53ef\u4ee5\u4ee3\u66ff\u539f\u6765\u7684os.path\u3002\u672c\u6587\u63d0\u4f9b\u51e0\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\u4f9b\u67e5\u8be2\uff0c\u5e2e\u60a8\u6781\u901f\u4e0a\u624b\uff0c\u5f15\u7528\u5230\u81ea\u5df1\u7684python project\u91cc\u3002 \u521b\u5efa\u64cd\u4f5cPath\u5bf9\u8c61\u4f8b\u5b50 \u53ef\u4ee5\u7528\u201d\/\u201c\u76f4\u63a5\u5f97\u5230\u4e0b\u4e00\u5c42\u76ee\u5f55\u6216\u6587\u4ef6\u7684Path\u5bf9\u8c61 Home Path\u5bf9\u8c61\u4f8b\u5b50 \u9700\u8981\u6ce8\u610f\u7684\u662f\uff0cPath constructor\u7684\u53c2\u6570\u4e0d\u8ba4\u8bc6~\u3002\u9700\u8981\u7528Path.home()\u3002 \u9a8c\u8bc1tilda\u4e0dwork \u9a8c\u8bc1Path.home() work \u5f53\u524d\u76ee\u5f55\u4f8b\u5b50 \u5982\u679c\u9700\u8981\u5f53\u524d\u76ee\u5f55\u7684\u5bf9\u8c61\u53ef\u4ee5\u7528cwd\u51fd\u6570\u6216\u8005.\u52a0resolve\u51fd\u6570 \u904d\u5386\u6587\u4ef6\u5939\u4f8b\u5b50 \u4e5f\u5c31\u662fimplement bash\u547d\u4ee4 \u7528pathlib\u5b9e\u73b0\u5982\u4e0b \u5916\u9762\u51fd\u6570python_find\u63a5\u53d7path string\uff0c\u91cc\u9762\u7684\u9012\u5f52\u51fd\u6570_python_find\u63a5\u53d7pathlib.Path object\u3002\u7528iterdir\u5217\u51fa\u5f53\u524dinput path\u4e0b\u7684\u6240\u6709\u5bf9\u8c61\uff0c\u5148\u6253\u5370\u5b83\u4eec\u7684\u8def\u5f84\uff0c\u5982\u679c\u662f\u76ee\u5f55\uff0c\u5c31\u7ee7\u7eed\u8c03\u7528\u9012\u5f52\u51fd\u6570_python_find\u6253\u5370\u4e0b\u4e00\u5c42\u76ee\u5f55\u91cc\u9762\u7684\u5185\u5bb9\u3002 Pathlib\u5220\u9664\u6587\u4ef6\u4f8b\u5b50 \u5220\u9664\u6587\u4ef6 \u5220\u9664\u76ee\u5f55\/\u6587\u4ef6\u5939 rmdir\u53ea\u80fd\u5220\u9664\u7a7a\u7684\u6587\u4ef6\u5939\/\u76ee\u5f55\uff0c\u5982\u679c\u5185\u90e8\u8fd8\u6709\u5176\u4ed6\u6587\u4ef6\u6216\u76ee\u5f55\u4f1a\u62a5\u9519\u3002 \u5220\u9664\u975e\u7a7a\u76ee\u5f55\/\u6587\u4ef6\u5939 \u8fd9\u4e2a\u53ea\u80fd\u81ea\u5df1\u7528\u4e0a\u9762\u4e24\u4e2a\u51fd\u6570\u7528\u9012\u5f52\u5b9e\u73b0\u3002\u8fd9\u91cc\u6211\u589e\u52a0\u4e86\u663e\u793a\u88ab\u5220\u9664\u6587\u4ef6size\u7684\u4fe1\u606f\u3002 \u53c2\u8003 \u4e00\u5206\u949f\u5b66\u4e00\u4e2a Python \u6807\u51c6\u5e93\u4e4b Pathlib PathLib recursively remove directory? pathlib&nbsp;\u2014 Object-oriented filesystem paths...Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/feellikelearning.com\/index.php\/2022\/07\/11\/python-pathlib-survival-guide-a-few-examples-to-learn\/?variant=zh-hant\" \/>\n<meta property=\"og:site_name\" content=\"Feel Like Learning\" \/>\n<meta property=\"article:published_time\" content=\"2022-07-11T18:55:06+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-12-10T07:35:24+00:00\" \/>\n<meta name=\"author\" content=\"feellikelearning\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"feellikelearning\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/feellikelearning.com\/index.php\/2022\/07\/11\/python-pathlib-survival-guide-a-few-examples-to-learn\/?variant=zh-hant#article\",\"isPartOf\":{\"@id\":\"https:\/\/feellikelearning.com\/index.php\/2022\/07\/11\/python-pathlib-survival-guide-a-few-examples-to-learn\/?variant=zh-hant\"},\"author\":{\"name\":\"feellikelearning\",\"@id\":\"https:\/\/feellikelearning.com\/#\/schema\/person\/91fb815bebebf166c217b5e3764d437a\"},\"headline\":\"Python Pathlib\u751f\u5b58\u624b\u518c\uff0c\u51e0\u4e2a\u77ed\u4f8b\u5b50\u6781\u901f\u4e0a\u624b\",\"datePublished\":\"2022-07-11T18:55:06+00:00\",\"dateModified\":\"2022-12-10T07:35:24+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/feellikelearning.com\/index.php\/2022\/07\/11\/python-pathlib-survival-guide-a-few-examples-to-learn\/?variant=zh-hant\"},\"wordCount\":49,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/feellikelearning.com\/#\/schema\/person\/91fb815bebebf166c217b5e3764d437a\"},\"articleSection\":[\"Python\",\"\u7f16\u7a0b\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/feellikelearning.com\/index.php\/2022\/07\/11\/python-pathlib-survival-guide-a-few-examples-to-learn\/?variant=zh-hant#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/feellikelearning.com\/index.php\/2022\/07\/11\/python-pathlib-survival-guide-a-few-examples-to-learn\/?variant=zh-hant\",\"url\":\"https:\/\/feellikelearning.com\/index.php\/2022\/07\/11\/python-pathlib-survival-guide-a-few-examples-to-learn\/?variant=zh-hant\",\"name\":\"Python Pathlib\u751f\u5b58\u624b\u518c\uff0c\u51e0\u4e2a\u77ed\u4f8b\u5b50\u6781\u901f\u4e0a\u624b | Feel Like Learning\",\"isPartOf\":{\"@id\":\"https:\/\/feellikelearning.com\/#website\"},\"datePublished\":\"2022-07-11T18:55:06+00:00\",\"dateModified\":\"2022-12-10T07:35:24+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/feellikelearning.com\/index.php\/2022\/07\/11\/python-pathlib-survival-guide-a-few-examples-to-learn\/?variant=zh-hant#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/feellikelearning.com\/index.php\/2022\/07\/11\/python-pathlib-survival-guide-a-few-examples-to-learn\/?variant=zh-hant\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/feellikelearning.com\/index.php\/2022\/07\/11\/python-pathlib-survival-guide-a-few-examples-to-learn\/?variant=zh-hant#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/feellikelearning.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python Pathlib\u751f\u5b58\u624b\u518c\uff0c\u51e0\u4e2a\u77ed\u4f8b\u5b50\u6781\u901f\u4e0a\u624b\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/feellikelearning.com\/#website\",\"url\":\"https:\/\/feellikelearning.com\/\",\"name\":\"Feel Like Learning\",\"description\":\"\u7a0b\u5e8f\uff5c\u751f\u6d3b\uff5c\u5b66\u5230\u5c31\u662f\u8d5a\u5230\",\"publisher\":{\"@id\":\"https:\/\/feellikelearning.com\/#\/schema\/person\/91fb815bebebf166c217b5e3764d437a\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/feellikelearning.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/feellikelearning.com\/#\/schema\/person\/91fb815bebebf166c217b5e3764d437a\",\"name\":\"feellikelearning\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/feellikelearning.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/72a1e86e9dcb0332e88bd7d54fd36c28?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/72a1e86e9dcb0332e88bd7d54fd36c28?s=96&d=mm&r=g\",\"caption\":\"feellikelearning\"},\"logo\":{\"@id\":\"https:\/\/feellikelearning.com\/#\/schema\/person\/image\/\"},\"url\":\"https:\/\/feellikelearning.com\/index.php\/author\/feellikelearning\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python Pathlib\u751f\u5b58\u624b\u518c\uff0c\u51e0\u4e2a\u77ed\u4f8b\u5b50\u6781\u901f\u4e0a\u624b | Feel Like Learning","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/feellikelearning.com\/index.php\/2022\/07\/11\/python-pathlib-survival-guide-a-few-examples-to-learn\/?variant=zh-hant","og_locale":"en_US","og_type":"article","og_title":"Python Pathlib\u751f\u5b58\u624b\u518c\uff0c\u51e0\u4e2a\u77ed\u4f8b\u5b50\u6781\u901f\u4e0a\u624b | Feel Like Learning","og_description":"Pathlib\u662fPython\u91cc\u81ea\u5e26\u7684\u5904\u7406OS\u8def\u5f84\u7684library\uff0c\u529f\u80fd\u975e\u5e38\u5f3a\u5927\uff0c\u53ef\u4ee5\u4ee3\u66ff\u539f\u6765\u7684os.path\u3002\u672c\u6587\u63d0\u4f9b\u51e0\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\u4f9b\u67e5\u8be2\uff0c\u5e2e\u60a8\u6781\u901f\u4e0a\u624b\uff0c\u5f15\u7528\u5230\u81ea\u5df1\u7684python project\u91cc\u3002 \u521b\u5efa\u64cd\u4f5cPath\u5bf9\u8c61\u4f8b\u5b50 \u53ef\u4ee5\u7528\u201d\/\u201c\u76f4\u63a5\u5f97\u5230\u4e0b\u4e00\u5c42\u76ee\u5f55\u6216\u6587\u4ef6\u7684Path\u5bf9\u8c61 Home Path\u5bf9\u8c61\u4f8b\u5b50 \u9700\u8981\u6ce8\u610f\u7684\u662f\uff0cPath constructor\u7684\u53c2\u6570\u4e0d\u8ba4\u8bc6~\u3002\u9700\u8981\u7528Path.home()\u3002 \u9a8c\u8bc1tilda\u4e0dwork \u9a8c\u8bc1Path.home() work \u5f53\u524d\u76ee\u5f55\u4f8b\u5b50 \u5982\u679c\u9700\u8981\u5f53\u524d\u76ee\u5f55\u7684\u5bf9\u8c61\u53ef\u4ee5\u7528cwd\u51fd\u6570\u6216\u8005.\u52a0resolve\u51fd\u6570 \u904d\u5386\u6587\u4ef6\u5939\u4f8b\u5b50 \u4e5f\u5c31\u662fimplement bash\u547d\u4ee4 \u7528pathlib\u5b9e\u73b0\u5982\u4e0b \u5916\u9762\u51fd\u6570python_find\u63a5\u53d7path string\uff0c\u91cc\u9762\u7684\u9012\u5f52\u51fd\u6570_python_find\u63a5\u53d7pathlib.Path object\u3002\u7528iterdir\u5217\u51fa\u5f53\u524dinput path\u4e0b\u7684\u6240\u6709\u5bf9\u8c61\uff0c\u5148\u6253\u5370\u5b83\u4eec\u7684\u8def\u5f84\uff0c\u5982\u679c\u662f\u76ee\u5f55\uff0c\u5c31\u7ee7\u7eed\u8c03\u7528\u9012\u5f52\u51fd\u6570_python_find\u6253\u5370\u4e0b\u4e00\u5c42\u76ee\u5f55\u91cc\u9762\u7684\u5185\u5bb9\u3002 Pathlib\u5220\u9664\u6587\u4ef6\u4f8b\u5b50 \u5220\u9664\u6587\u4ef6 \u5220\u9664\u76ee\u5f55\/\u6587\u4ef6\u5939 rmdir\u53ea\u80fd\u5220\u9664\u7a7a\u7684\u6587\u4ef6\u5939\/\u76ee\u5f55\uff0c\u5982\u679c\u5185\u90e8\u8fd8\u6709\u5176\u4ed6\u6587\u4ef6\u6216\u76ee\u5f55\u4f1a\u62a5\u9519\u3002 \u5220\u9664\u975e\u7a7a\u76ee\u5f55\/\u6587\u4ef6\u5939 \u8fd9\u4e2a\u53ea\u80fd\u81ea\u5df1\u7528\u4e0a\u9762\u4e24\u4e2a\u51fd\u6570\u7528\u9012\u5f52\u5b9e\u73b0\u3002\u8fd9\u91cc\u6211\u589e\u52a0\u4e86\u663e\u793a\u88ab\u5220\u9664\u6587\u4ef6size\u7684\u4fe1\u606f\u3002 \u53c2\u8003 \u4e00\u5206\u949f\u5b66\u4e00\u4e2a Python \u6807\u51c6\u5e93\u4e4b Pathlib PathLib recursively remove directory? pathlib&nbsp;\u2014 Object-oriented filesystem paths...Read more","og_url":"https:\/\/feellikelearning.com\/index.php\/2022\/07\/11\/python-pathlib-survival-guide-a-few-examples-to-learn\/?variant=zh-hant","og_site_name":"Feel Like Learning","article_published_time":"2022-07-11T18:55:06+00:00","article_modified_time":"2022-12-10T07:35:24+00:00","author":"feellikelearning","twitter_card":"summary_large_image","twitter_misc":{"Written by":"feellikelearning","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/feellikelearning.com\/index.php\/2022\/07\/11\/python-pathlib-survival-guide-a-few-examples-to-learn\/?variant=zh-hant#article","isPartOf":{"@id":"https:\/\/feellikelearning.com\/index.php\/2022\/07\/11\/python-pathlib-survival-guide-a-few-examples-to-learn\/?variant=zh-hant"},"author":{"name":"feellikelearning","@id":"https:\/\/feellikelearning.com\/#\/schema\/person\/91fb815bebebf166c217b5e3764d437a"},"headline":"Python Pathlib\u751f\u5b58\u624b\u518c\uff0c\u51e0\u4e2a\u77ed\u4f8b\u5b50\u6781\u901f\u4e0a\u624b","datePublished":"2022-07-11T18:55:06+00:00","dateModified":"2022-12-10T07:35:24+00:00","mainEntityOfPage":{"@id":"https:\/\/feellikelearning.com\/index.php\/2022\/07\/11\/python-pathlib-survival-guide-a-few-examples-to-learn\/?variant=zh-hant"},"wordCount":49,"commentCount":0,"publisher":{"@id":"https:\/\/feellikelearning.com\/#\/schema\/person\/91fb815bebebf166c217b5e3764d437a"},"articleSection":["Python","\u7f16\u7a0b"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/feellikelearning.com\/index.php\/2022\/07\/11\/python-pathlib-survival-guide-a-few-examples-to-learn\/?variant=zh-hant#respond"]}]},{"@type":"WebPage","@id":"https:\/\/feellikelearning.com\/index.php\/2022\/07\/11\/python-pathlib-survival-guide-a-few-examples-to-learn\/?variant=zh-hant","url":"https:\/\/feellikelearning.com\/index.php\/2022\/07\/11\/python-pathlib-survival-guide-a-few-examples-to-learn\/?variant=zh-hant","name":"Python Pathlib\u751f\u5b58\u624b\u518c\uff0c\u51e0\u4e2a\u77ed\u4f8b\u5b50\u6781\u901f\u4e0a\u624b | Feel Like Learning","isPartOf":{"@id":"https:\/\/feellikelearning.com\/#website"},"datePublished":"2022-07-11T18:55:06+00:00","dateModified":"2022-12-10T07:35:24+00:00","breadcrumb":{"@id":"https:\/\/feellikelearning.com\/index.php\/2022\/07\/11\/python-pathlib-survival-guide-a-few-examples-to-learn\/?variant=zh-hant#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/feellikelearning.com\/index.php\/2022\/07\/11\/python-pathlib-survival-guide-a-few-examples-to-learn\/?variant=zh-hant"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/feellikelearning.com\/index.php\/2022\/07\/11\/python-pathlib-survival-guide-a-few-examples-to-learn\/?variant=zh-hant#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/feellikelearning.com\/"},{"@type":"ListItem","position":2,"name":"Python Pathlib\u751f\u5b58\u624b\u518c\uff0c\u51e0\u4e2a\u77ed\u4f8b\u5b50\u6781\u901f\u4e0a\u624b"}]},{"@type":"WebSite","@id":"https:\/\/feellikelearning.com\/#website","url":"https:\/\/feellikelearning.com\/","name":"Feel Like Learning","description":"\u7a0b\u5e8f\uff5c\u751f\u6d3b\uff5c\u5b66\u5230\u5c31\u662f\u8d5a\u5230","publisher":{"@id":"https:\/\/feellikelearning.com\/#\/schema\/person\/91fb815bebebf166c217b5e3764d437a"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/feellikelearning.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/feellikelearning.com\/#\/schema\/person\/91fb815bebebf166c217b5e3764d437a","name":"feellikelearning","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/feellikelearning.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/72a1e86e9dcb0332e88bd7d54fd36c28?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/72a1e86e9dcb0332e88bd7d54fd36c28?s=96&d=mm&r=g","caption":"feellikelearning"},"logo":{"@id":"https:\/\/feellikelearning.com\/#\/schema\/person\/image\/"},"url":"https:\/\/feellikelearning.com\/index.php\/author\/feellikelearning\/"}]}},"_links":{"self":[{"href":"https:\/\/feellikelearning.com\/index.php\/wp-json\/wp\/v2\/posts\/1437"}],"collection":[{"href":"https:\/\/feellikelearning.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/feellikelearning.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/feellikelearning.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/feellikelearning.com\/index.php\/wp-json\/wp\/v2\/comments?post=1437"}],"version-history":[{"count":21,"href":"https:\/\/feellikelearning.com\/index.php\/wp-json\/wp\/v2\/posts\/1437\/revisions"}],"predecessor-version":[{"id":1804,"href":"https:\/\/feellikelearning.com\/index.php\/wp-json\/wp\/v2\/posts\/1437\/revisions\/1804"}],"wp:attachment":[{"href":"https:\/\/feellikelearning.com\/index.php\/wp-json\/wp\/v2\/media?parent=1437"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/feellikelearning.com\/index.php\/wp-json\/wp\/v2\/categories?post=1437"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/feellikelearning.com\/index.php\/wp-json\/wp\/v2\/tags?post=1437"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}