{"id":970747,"date":"2024-12-27T05:28:42","date_gmt":"2024-12-26T21:28:42","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/970747.html"},"modified":"2024-12-27T05:28:43","modified_gmt":"2024-12-26T21:28:43","slug":"python%e5%a6%82%e4%bd%95%e6%9f%a5%e6%89%be%e7%9b%ae%e5%bd%95%e4%bd%8d%e7%bd%ae","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/970747.html","title":{"rendered":"python\u5982\u4f55\u67e5\u627e\u76ee\u5f55\u4f4d\u7f6e"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24193927\/e08da094-ff23-431d-a082-2a9892d63e9e.webp\" alt=\"python\u5982\u4f55\u67e5\u627e\u76ee\u5f55\u4f4d\u7f6e\" \/><\/p>\n<p><p> <strong>Python\u4e2d\u67e5\u627e\u76ee\u5f55\u4f4d\u7f6e\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528os\u6a21\u5757\u3001\u4f7f\u7528pathlib\u6a21\u5757\u3001\u4f7f\u7528os.path\u6a21\u5757\u3002<\/strong>\u8fd9\u4e9b\u65b9\u6cd5\u5404\u6709\u7279\u70b9\uff0c\u5176\u4e2d\uff0cos\u6a21\u5757\u662fPython\u7684\u6807\u51c6\u5e93\u6a21\u5757\uff0c\u63d0\u4f9b\u4e86\u4e0e\u64cd\u4f5c\u7cfb\u7edf\u8fdb\u884c\u4ea4\u4e92\u7684\u529f\u80fd\uff1bpathlib\u6a21\u5757\u63d0\u4f9b\u4e86\u9762\u5411\u5bf9\u8c61\u7684\u8def\u5f84\u64cd\u4f5c\u65b9\u6cd5\uff1bos.path\u6a21\u5757\u5219\u4e13\u6ce8\u4e8e\u8def\u5f84\u540d\u7684\u64cd\u4f5c\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4e2d\u4e00\u79cd\u65b9\u6cd5\uff0c\u5373\u4f7f\u7528os\u6a21\u5757\u67e5\u627e\u76ee\u5f55\u4f4d\u7f6e\u3002<\/p>\n<\/p>\n<p><p>os\u6a21\u5757\u662fPython\u7684\u6807\u51c6\u5e93\u6a21\u5757\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e00\u4e9b\u4e0e\u64cd\u4f5c\u7cfb\u7edf\u4ea4\u4e92\u7684\u51fd\u6570\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528os\u6a21\u5757\u4e2d\u7684<code>os.getcwd()<\/code>\u51fd\u6570\u6765\u83b7\u53d6\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\uff0c\u4f7f\u7528<code>os.listdir()<\/code>\u51fd\u6570\u6765\u5217\u51fa\u6307\u5b9a\u76ee\u5f55\u4e2d\u7684\u6587\u4ef6\u548c\u5b50\u76ee\u5f55\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528<code>os.path.join()<\/code>\u51fd\u6570\u6765\u6784\u5efa\u76ee\u5f55\u8def\u5f84\u3002\u6b64\u5916\uff0cos\u6a21\u5757\u8fd8\u63d0\u4f9b\u4e86\u8bf8\u5982<code>os.path.abspath()<\/code>\u7528\u6765\u83b7\u53d6\u7edd\u5bf9\u8def\u5f84\u7684\u51fd\u6570\uff0c\u8fd9\u5728\u9700\u8981\u5904\u7406\u76f8\u5bf9\u8def\u5f84\u65f6\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001OS\u6a21\u5757\u7684\u4f7f\u7528<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0cos\u6a21\u5757\u662f\u4e00\u4e2a\u529f\u80fd\u4e30\u5bcc\u7684\u5e93\uff0c\u4e3b\u8981\u7528\u4e8e\u4e0e\u64cd\u4f5c\u7cfb\u7edf\u8fdb\u884c\u4ea4\u4e92\u3002\u5b83\u4e3a\u6211\u4eec\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u67e5\u627e\u548c\u64cd\u4f5c\u76ee\u5f55\u4f4d\u7f6e\u3002<\/p>\n<\/p>\n<p><h3>1. \u83b7\u53d6\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55<\/h3>\n<\/p>\n<p><p>\u8981\u83b7\u53d6\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\uff0c\u53ef\u4ee5\u4f7f\u7528<code>os.getcwd()<\/code>\u51fd\u6570\u3002\u8fd9\u4e2a\u51fd\u6570\u4e0d\u9700\u8981\u4efb\u4f55\u53c2\u6570\uff0c\u76f4\u63a5\u8fd4\u56de\u4e00\u4e2a\u5b57\u7b26\u4e32\u5f62\u5f0f\u7684\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u83b7\u53d6\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55<\/strong><\/h2>\n<p>current_directory = os.getcwd()<\/p>\n<p>print(&quot;\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u4e3a\uff1a&quot;, current_directory)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u5217\u51fa\u76ee\u5f55\u5185\u5bb9<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>os.listdir()<\/code>\u51fd\u6570\u6765\u5217\u51fa\u6307\u5b9a\u76ee\u5f55\u4e2d\u7684\u6240\u6709\u6587\u4ef6\u548c\u5b50\u76ee\u5f55\u3002\u8fd9\u4e2a\u51fd\u6570\u63a5\u53d7\u4e00\u4e2a\u76ee\u5f55\u8def\u5f84\u4f5c\u4e3a\u53c2\u6570\uff0c\u5e76\u8fd4\u56de\u8be5\u76ee\u5f55\u4e2d\u6240\u6709\u6587\u4ef6\u548c\u5b50\u76ee\u5f55\u7684\u540d\u79f0\u5217\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5217\u51fa\u6307\u5b9a\u76ee\u5f55\u4e2d\u7684\u5185\u5bb9<\/p>\n<p>directory_contents = os.listdir(current_directory)<\/p>\n<p>print(&quot;\u76ee\u5f55\u5185\u5bb9\uff1a&quot;, directory_contents)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3. \u6784\u5efa\u76ee\u5f55\u8def\u5f84<\/h3>\n<\/p>\n<p><p><code>os.path.join()<\/code>\u51fd\u6570\u7528\u4e8e\u5c06\u591a\u4e2a\u8def\u5f84\u7ec4\u5408\u5728\u4e00\u8d77\u3002\u5b83\u4f1a\u6839\u636e\u64cd\u4f5c\u7cfb\u7edf\u81ea\u52a8\u9009\u62e9\u5408\u9002\u7684\u8def\u5f84\u5206\u9694\u7b26\uff0c\u4ece\u800c\u907f\u514d\u4e86\u624b\u52a8\u6dfb\u52a0\u5206\u9694\u7b26\u7684\u9ebb\u70e6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6784\u5efa\u65b0\u7684\u76ee\u5f55\u8def\u5f84<\/p>\n<p>new_path = os.path.join(current_directory, &#39;\u5b50\u76ee\u5f55&#39;)<\/p>\n<p>print(&quot;\u65b0\u7684\u76ee\u5f55\u8def\u5f84\u4e3a\uff1a&quot;, new_path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>4. \u83b7\u53d6\u7edd\u5bf9\u8def\u5f84<\/h3>\n<\/p>\n<p><p><code>os.path.abspath()<\/code>\u51fd\u6570\u53ef\u4ee5\u5c06\u4e00\u4e2a\u76f8\u5bf9\u8def\u5f84\u8f6c\u6362\u4e3a\u7edd\u5bf9\u8def\u5f84\u3002\u8fd9\u5728\u9700\u8981\u5904\u7406\u76f8\u5bf9\u8def\u5f84\u65f6\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u83b7\u53d6\u67d0\u4e2a\u6587\u4ef6\u7684\u7edd\u5bf9\u8def\u5f84<\/p>\n<p>relative_path = &#39;example.txt&#39;<\/p>\n<p>absolute_path = os.path.abspath(relative_path)<\/p>\n<p>print(&quot;\u7edd\u5bf9\u8def\u5f84\u4e3a\uff1a&quot;, absolute_path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>5. \u68c0\u67e5\u8def\u5f84\u662f\u5426\u5b58\u5728<\/h3>\n<\/p>\n<p><p><code>os.path.exists()<\/code>\u51fd\u6570\u53ef\u4ee5\u7528\u6765\u68c0\u67e5\u4e00\u4e2a\u8def\u5f84\u662f\u5426\u5b58\u5728\uff0c\u8fd4\u56de\u503c\u4e3aTrue\u6216False\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u68c0\u67e5\u8def\u5f84\u662f\u5426\u5b58\u5728<\/p>\n<p>path_exists = os.path.exists(new_path)<\/p>\n<p>print(&quot;\u8def\u5f84\u662f\u5426\u5b58\u5728\uff1a&quot;, path_exists)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7os\u6a21\u5757\uff0c\u6211\u4eec\u53ef\u4ee5\u7075\u6d3b\u5730\u83b7\u53d6\u548c\u64cd\u4f5c\u76ee\u5f55\u4f4d\u7f6e\uff0c\u4ece\u800c\u66f4\u597d\u5730\u7ba1\u7406\u6587\u4ef6\u548c\u76ee\u5f55\u7ed3\u6784\u3002os\u6a21\u5757\u7684\u8fd9\u4e9b\u529f\u80fd\u5728\u5904\u7406\u6587\u4ef6\u7cfb\u7edf\u76f8\u5173\u4efb\u52a1\u65f6\u5c24\u4e3a\u91cd\u8981\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001PATHLIB\u6a21\u5757\u7684\u4f7f\u7528<\/p>\n<\/p>\n<p><p>pathlib\u6a21\u5757\u662fPython 3.4\u5f15\u5165\u7684\u65b0\u6a21\u5757\uff0c\u63d0\u4f9b\u4e86\u9762\u5411\u5bf9\u8c61\u7684\u8def\u5f84\u64cd\u4f5c\u65b9\u6cd5\u3002\u4e0eos\u6a21\u5757\u76f8\u6bd4\uff0cpathlib\u6a21\u5757\u7684\u63a5\u53e3\u66f4\u52a0\u76f4\u89c2\u548c\u6613\u4e8e\u4f7f\u7528\uff0c\u7279\u522b\u662f\u5728\u5904\u7406\u8def\u5f84\u64cd\u4f5c\u65f6\u3002<\/p>\n<\/p>\n<p><h3>1. \u521b\u5efa\u8def\u5f84\u5bf9\u8c61<\/h3>\n<\/p>\n<p><p>pathlib\u6a21\u5757\u7684\u6838\u5fc3\u662fPath\u7c7b\uff0c\u5b83\u7528\u4e8e\u8868\u793a\u548c\u64cd\u4f5c\u8def\u5f84\u3002\u8981\u4f7f\u7528\u5b83\uff0c\u9996\u5148\u9700\u8981\u5bfc\u5165pathlib\u6a21\u5757\u5e76\u521b\u5efa\u4e00\u4e2aPath\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<h2><strong>\u521b\u5efa\u8def\u5f84\u5bf9\u8c61<\/strong><\/h2>\n<p>current_path = Path(&#39;.&#39;)<\/p>\n<p>print(&quot;\u5f53\u524d\u8def\u5f84\u5bf9\u8c61\uff1a&quot;, current_path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u83b7\u53d6\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528Path\u5bf9\u8c61\u7684<code>resolve()<\/code>\u65b9\u6cd5\u83b7\u53d6\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u7684\u7edd\u5bf9\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u83b7\u53d6\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u7684\u7edd\u5bf9\u8def\u5f84<\/p>\n<p>absolute_current_path = current_path.resolve()<\/p>\n<p>print(&quot;\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u7684\u7edd\u5bf9\u8def\u5f84\u4e3a\uff1a&quot;, absolute_current_path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3. \u5217\u51fa\u76ee\u5f55\u5185\u5bb9<\/h3>\n<\/p>\n<p><p>\u4e0eos\u6a21\u5757\u7c7b\u4f3c\uff0c\u53ef\u4ee5\u4f7f\u7528Path\u5bf9\u8c61\u7684<code>iterdir()<\/code>\u65b9\u6cd5\u5217\u51fa\u76ee\u5f55\u4e2d\u7684\u6587\u4ef6\u548c\u5b50\u76ee\u5f55\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5217\u51fa\u76ee\u5f55\u5185\u5bb9<\/p>\n<p>for item in current_path.iterdir():<\/p>\n<p>    print(&quot;\u76ee\u5f55\u5185\u5bb9\u9879\uff1a&quot;, item)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>4. \u6784\u5efa\u76ee\u5f55\u8def\u5f84<\/h3>\n<\/p>\n<p><p>Path\u5bf9\u8c61\u652f\u6301\u4f7f\u7528<code>\/<\/code>\u8fd0\u7b97\u7b26\u6765\u6784\u5efa\u65b0\u7684\u76ee\u5f55\u8def\u5f84\uff0c\u8fd9\u4f7f\u5f97\u8def\u5f84\u64cd\u4f5c\u66f4\u52a0\u7b80\u6d01\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6784\u5efa\u65b0\u7684\u76ee\u5f55\u8def\u5f84<\/p>\n<p>new_path = current_path \/ &#39;\u5b50\u76ee\u5f55&#39;<\/p>\n<p>print(&quot;\u65b0\u7684\u76ee\u5f55\u8def\u5f84\u4e3a\uff1a&quot;, new_path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>5. \u68c0\u67e5\u8def\u5f84\u662f\u5426\u5b58\u5728<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528Path\u5bf9\u8c61\u7684<code>exists()<\/code>\u65b9\u6cd5\u68c0\u67e5\u4e00\u4e2a\u8def\u5f84\u662f\u5426\u5b58\u5728\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u68c0\u67e5\u8def\u5f84\u662f\u5426\u5b58\u5728<\/p>\n<p>path_exists = new_path.exists()<\/p>\n<p>print(&quot;\u8def\u5f84\u662f\u5426\u5b58\u5728\uff1a&quot;, path_exists)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>pathlib\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u79cd\u66f4\u73b0\u4ee3\u548c\u76f4\u89c2\u7684\u65b9\u6cd5\u6765\u64cd\u4f5c\u8def\u5f84\u3002\u5b83\u7684Path\u5bf9\u8c61\u652f\u6301\u94fe\u5f0f\u8c03\u7528\u548c\u8fd0\u7b97\u7b26\u91cd\u8f7d\uff0c\u4f7f\u5f97\u8def\u5f84\u64cd\u4f5c\u66f4\u52a0\u81ea\u7136\u548c\u7b80\u6d01\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001OS.PATH\u6a21\u5757\u7684\u4f7f\u7528<\/p>\n<\/p>\n<p><p>os.path\u6a21\u5757\u4e13\u6ce8\u4e8e\u8def\u5f84\u540d\u7684\u64cd\u4f5c\uff0c\u63d0\u4f9b\u4e86\u4e00\u4e9b\u51fd\u6570\u7528\u4e8e\u89e3\u6790\u548c\u64cd\u4f5c\u8def\u5f84\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><h3>1. \u5206\u5272\u8def\u5f84<\/h3>\n<\/p>\n<p><p><code>os.path.split()<\/code>\u51fd\u6570\u53ef\u4ee5\u5c06\u8def\u5f84\u5206\u5272\u4e3a\u76ee\u5f55\u548c\u6587\u4ef6\u540d\u4e24\u90e8\u5206\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u5206\u5272\u8def\u5f84<\/strong><\/h2>\n<p>path = &#39;\/home\/user\/example.txt&#39;<\/p>\n<p>directory, filename = os.path.split(path)<\/p>\n<p>print(&quot;\u76ee\u5f55\u90e8\u5206\uff1a&quot;, directory)<\/p>\n<p>print(&quot;\u6587\u4ef6\u540d\u90e8\u5206\uff1a&quot;, filename)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u83b7\u53d6\u6587\u4ef6\u6269\u5c55\u540d<\/h3>\n<\/p>\n<p><p><code>os.path.splitext()<\/code>\u51fd\u6570\u53ef\u4ee5\u5c06\u8def\u5f84\u5206\u5272\u4e3a\u6587\u4ef6\u540d\u548c\u6269\u5c55\u540d\u4e24\u90e8\u5206\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u83b7\u53d6\u6587\u4ef6\u6269\u5c55\u540d<\/p>\n<p>filename, file_extension = os.path.splitext(path)<\/p>\n<p>print(&quot;\u6587\u4ef6\u540d\uff1a&quot;, filename)<\/p>\n<p>print(&quot;\u6587\u4ef6\u6269\u5c55\u540d\uff1a&quot;, file_extension)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3. \u68c0\u67e5\u662f\u5426\u4e3a\u76ee\u5f55\u6216\u6587\u4ef6<\/h3>\n<\/p>\n<p><p><code>os.path.isdir()<\/code>\u548c<code>os.path.isfile()<\/code>\u51fd\u6570\u53ef\u4ee5\u5206\u522b\u68c0\u67e5\u4e00\u4e2a\u8def\u5f84\u662f\u5426\u4e3a\u76ee\u5f55\u6216\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u68c0\u67e5\u662f\u5426\u4e3a\u76ee\u5f55\u6216\u6587\u4ef6<\/p>\n<p>is_directory = os.path.isdir(directory)<\/p>\n<p>is_file = os.path.isfile(path)<\/p>\n<p>print(&quot;\u662f\u5426\u4e3a\u76ee\u5f55\uff1a&quot;, is_directory)<\/p>\n<p>print(&quot;\u662f\u5426\u4e3a\u6587\u4ef6\uff1a&quot;, is_file)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>os.path\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u4e9b\u57fa\u672c\u7684\u8def\u5f84\u64cd\u4f5c\u51fd\u6570\uff0c\u9002\u5408\u5904\u7406\u7b80\u5355\u7684\u8def\u5f84\u89e3\u6790\u4efb\u52a1\u3002\u5728\u590d\u6742\u8def\u5f84\u64cd\u4f5c\u9700\u6c42\u4e0b\uff0c\u7ed3\u5408os\u6a21\u5757\u6216pathlib\u6a21\u5757\u4f7f\u7528\u6548\u679c\u66f4\u4f73\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u67e5\u627e\u548c\u64cd\u4f5c\u76ee\u5f55\u4f4d\u7f6e\uff0c\u5305\u62ecos\u6a21\u5757\u3001pathlib\u6a21\u5757\u548cos.path\u6a21\u5757\u3002<strong>os\u6a21\u5757\u63d0\u4f9b\u4e86\u4e0e\u64cd\u4f5c\u7cfb\u7edf\u4ea4\u4e92\u7684\u57fa\u672c\u529f\u80fd<\/strong>\uff0c\u9002\u5408\u5904\u7406\u7b80\u5355\u7684\u76ee\u5f55\u64cd\u4f5c\uff1b<strong>pathlib\u6a21\u5757\u63d0\u4f9b\u4e86\u9762\u5411\u5bf9\u8c61\u7684\u8def\u5f84\u64cd\u4f5c<\/strong>\uff0c\u4f7f\u5f97\u4ee3\u7801\u66f4\u4e3a\u7b80\u6d01\u548c\u6613\u8bfb\uff1b<strong>os.path\u6a21\u5757\u4e13\u6ce8\u4e8e\u8def\u5f84\u540d\u7684\u64cd\u4f5c<\/strong>\uff0c\u9002\u5408\u5904\u7406\u8def\u5f84\u5b57\u7b26\u4e32\u89e3\u6790\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><p>\u5728\u4f7f\u7528\u8fd9\u4e9b\u6a21\u5757\u65f6\uff0c\u9700\u8981\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002\u4f8b\u5982\uff0c\u5728\u5904\u7406\u8f83\u4e3a\u590d\u6742\u7684\u8def\u5f84\u64cd\u4f5c\u65f6\uff0cpathlib\u6a21\u5757\u7684\u9762\u5411\u5bf9\u8c61\u63a5\u53e3\u53ef\u80fd\u66f4\u4e3a\u76f4\u89c2\u548c\u9ad8\u6548\uff0c\u800c\u5bf9\u4e8e\u7b80\u5355\u7684\u8def\u5f84\u68c0\u67e5\u4efb\u52a1\uff0cos.path\u6a21\u5757\u7684\u51fd\u6570\u53ef\u80fd\u66f4\u52a0\u76f4\u63a5\u548c\u6613\u7528\u3002\u901a\u8fc7\u5408\u7406\u9009\u62e9\u548c\u7ec4\u5408\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u66f4\u597d\u5730\u7ba1\u7406\u548c\u64cd\u4f5c\u6587\u4ef6\u7cfb\u7edf\u4e2d\u7684\u76ee\u5f55\u548c\u6587\u4ef6\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u83b7\u53d6\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>os<\/code>\u6a21\u5757\u4e2d\u7684<code>getcwd()<\/code>\u51fd\u6570\u6765\u83b7\u53d6\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import os\ncurrent_directory = os.getcwd()\nprint(&quot;\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u662f:&quot;, current_directory)\n<\/code><\/pre>\n<p>\u8be5\u51fd\u6570\u5c06\u8fd4\u56de\u5f53\u524d\u811a\u672c\u6267\u884c\u65f6\u7684\u76ee\u5f55\u8def\u5f84\u3002<\/p>\n<p><strong>\u5982\u4f55\u67e5\u627e\u7279\u5b9a\u6587\u4ef6\u7684\u7edd\u5bf9\u8def\u5f84\uff1f<\/strong><br \/>\u5982\u679c\u60f3\u8981\u627e\u5230\u7279\u5b9a\u6587\u4ef6\u7684\u7edd\u5bf9\u8def\u5f84\uff0c\u53ef\u4ee5\u4f7f\u7528<code>os.path.abspath()<\/code>\u51fd\u6570\u3002\u4f20\u5165\u6587\u4ef6\u540d\u6216\u76f8\u5bf9\u8def\u5f84\uff0c\u5b83\u5c06\u8fd4\u56de\u8be5\u6587\u4ef6\u7684\u7edd\u5bf9\u8def\u5f84\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">import os\nfile_path = &quot;example.txt&quot;\nabsolute_path = os.path.abspath(file_path)\nprint(&quot;\u6587\u4ef6\u7684\u7edd\u5bf9\u8def\u5f84\u662f:&quot;, absolute_path)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u80fd\u5e2e\u52a9\u4f60\u5feb\u901f\u5b9a\u4f4d\u6587\u4ef6\u5728\u7cfb\u7edf\u4e2d\u7684\u51c6\u786e\u4f4d\u7f6e\u3002<\/p>\n<p><strong>\u5982\u4f55\u5217\u51fa\u76ee\u5f55\u4e2d\u7684\u6240\u6709\u6587\u4ef6\u548c\u5b50\u76ee\u5f55\uff1f<\/strong><br \/>\u4f7f\u7528<code>os<\/code>\u6a21\u5757\u4e2d\u7684<code>listdir()<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5217\u51fa\u6307\u5b9a\u76ee\u5f55\u4e2d\u7684\u6240\u6709\u6587\u4ef6\u548c\u5b50\u76ee\u5f55\u3002\u4ee3\u7801\u793a\u4f8b\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import os\ndirectory_path = &quot;your_directory_path&quot;\nfiles_and_dirs = os.listdir(directory_path)\nprint(&quot;\u76ee\u5f55\u4e2d\u7684\u6587\u4ef6\u548c\u5b50\u76ee\u5f55\u6709:&quot;, files_and_dirs)\n<\/code><\/pre>\n<p>\u6b64\u65b9\u6cd5\u975e\u5e38\u9002\u5408\u7528\u4e8e\u67e5\u770b\u67d0\u4e2a\u76ee\u5f55\u4e0b\u7684\u6240\u6709\u5185\u5bb9\uff0c\u5e2e\u52a9\u7528\u6237\u5feb\u901f\u638c\u63e1\u76ee\u5f55\u7ed3\u6784\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4e2d\u67e5\u627e\u76ee\u5f55\u4f4d\u7f6e\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528os\u6a21\u5757\u3001\u4f7f\u7528pathlib\u6a21\u5757\u3001\u4f7f\u7528os.path\u6a21\u5757\u3002\u8fd9\u4e9b\u65b9\u6cd5\u5404 [&hellip;]","protected":false},"author":3,"featured_media":970748,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/970747"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=970747"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/970747\/revisions"}],"predecessor-version":[{"id":970749,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/970747\/revisions\/970749"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/970748"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=970747"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=970747"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=970747"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}