{"id":1182726,"date":"2025-01-15T19:06:35","date_gmt":"2025-01-15T11:06:35","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1182726.html"},"modified":"2025-01-15T19:06:37","modified_gmt":"2025-01-15T11:06:37","slug":"python%e5%a6%82%e4%bd%95%e6%9f%a5%e7%9c%8b%e5%bd%93%e5%89%8d%e8%b7%af%e5%be%84","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1182726.html","title":{"rendered":"python\u5982\u4f55\u67e5\u770b\u5f53\u524d\u8def\u5f84"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25130626\/2370ef88-d749-457b-9eb7-513ada5712d3.webp\" alt=\"python\u5982\u4f55\u67e5\u770b\u5f53\u524d\u8def\u5f84\" \/><\/p>\n<p><p> <strong>\u4f7f\u7528os\u6a21\u5757\u3001\u4f7f\u7528pathlib\u6a21\u5757\u3001\u533a\u5206\u76f8\u5bf9\u8def\u5f84\u548c\u7edd\u5bf9\u8def\u5f84<\/strong>\u3002\u5176\u4e2d\uff0c\u4f7f\u7528os\u6a21\u5757\u662f\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p><strong>os\u6a21\u5757<\/strong>\u662fPython\u6807\u51c6\u5e93\u4e2d\u7684\u4e00\u4e2a\u6a21\u5757\uff0c\u63d0\u4f9b\u4e86\u975e\u5e38\u4e30\u5bcc\u7684\u51fd\u6570\u548c\u65b9\u6cd5\u6765\u4e0e\u64cd\u4f5c\u7cfb\u7edf\u8fdb\u884c\u4ea4\u4e92\uff0c\u5305\u62ec\u6587\u4ef6\u548c\u76ee\u5f55\u7684\u64cd\u4f5c\u3002\u8981\u67e5\u770b\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\uff0c\u53ef\u4ee5\u4f7f\u7528<code>os.getcwd()<\/code>\u51fd\u6570\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u8be6\u7ec6\u8bf4\u660e\uff1a<\/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_path = os.getcwd()<\/p>\n<p>print(&quot;\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55:&quot;, current_path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>os.getcwd()<\/code>\u51fd\u6570\u8fd4\u56de\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u7684\u7edd\u5bf9\u8def\u5f84\uff0c\u8fd9\u662f\u4e00\u4e2a\u5b57\u7b26\u4e32\u7c7b\u578b\u7684\u6570\u636e\u3002\u5982\u679c\u4f60\u5728\u547d\u4ee4\u884c\u73af\u5883\u4e0b\u8fd0\u884c\u4e0a\u8ff0\u4ee3\u7801\uff0c\u5b83\u4f1a\u8f93\u51fa\u5f53\u524d\u7684\u5de5\u4f5c\u76ee\u5f55\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528os\u6a21\u5757<\/h3>\n<\/p>\n<p><h4>1\u3001\u83b7\u53d6\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55<\/h4>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0cos\u6a21\u5757\u63d0\u4f9b\u4e86\u8bb8\u591a\u65b9\u6cd5\u6765\u64cd\u4f5c\u6587\u4ef6\u548c\u76ee\u5f55\u3002\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\u8fd4\u56de\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u8868\u793a\u5f53\u524d\u7684\u5de5\u4f5c\u76ee\u5f55\u7684\u7edd\u5bf9\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><p>\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>current_path = os.getcwd()<\/p>\n<p>print(&quot;\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55:&quot;, current_path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u6539\u53d8\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55<\/h4>\n<\/p>\n<p><p>\u6709\u65f6\u5019\u6211\u4eec\u9700\u8981\u6539\u53d8\u5f53\u524d\u7684\u5de5\u4f5c\u76ee\u5f55\uff0c\u8fd9\u53ef\u4ee5\u901a\u8fc7<code>os.chdir()<\/code>\u51fd\u6570\u5b9e\u73b0\u3002\u8fd9\u4e2a\u51fd\u6570\u63a5\u53d7\u4e00\u4e2a\u8def\u5f84\u4f5c\u4e3a\u53c2\u6570\uff0c\u6539\u53d8\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u5230\u6307\u5b9a\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><p>\u793a\u4f8b\u4ee3\u7801\uff1a<\/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_path = os.getcwd()<\/p>\n<p>print(&quot;\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55:&quot;, current_path)<\/p>\n<h2><strong>\u6539\u53d8\u5de5\u4f5c\u76ee\u5f55<\/strong><\/h2>\n<p>new_path = &quot;\/path\/to\/new\/directory&quot;<\/p>\n<p>os.chdir(new_path)<\/p>\n<p>print(&quot;\u6539\u53d8\u540e\u7684\u5de5\u4f5c\u76ee\u5f55:&quot;, os.getcwd())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u521b\u5efa\u548c\u5220\u9664\u76ee\u5f55<\/h4>\n<\/p>\n<p><p>\u9664\u4e86\u83b7\u53d6\u548c\u6539\u53d8\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\uff0cos\u6a21\u5757\u8fd8\u63d0\u4f9b\u4e86\u521b\u5efa\u548c\u5220\u9664\u76ee\u5f55\u7684\u65b9\u6cd5\u3002<code>os.mkdir()<\/code>\u51fd\u6570\u7528\u4e8e\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u76ee\u5f55\uff0c\u800c<code>os.rmdir()<\/code>\u51fd\u6570\u7528\u4e8e\u5220\u9664\u4e00\u4e2a\u76ee\u5f55\u3002<\/p>\n<\/p>\n<p><p>\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u521b\u5efa\u65b0\u76ee\u5f55<\/strong><\/h2>\n<p>new_directory = &quot;new_directory&quot;<\/p>\n<p>os.mkdir(new_directory)<\/p>\n<p>print(f&quot;\u76ee\u5f55 {new_directory} \u521b\u5efa\u6210\u529f&quot;)<\/p>\n<h2><strong>\u5220\u9664\u76ee\u5f55<\/strong><\/h2>\n<p>os.rmdir(new_directory)<\/p>\n<p>print(f&quot;\u76ee\u5f55 {new_directory} \u5220\u9664\u6210\u529f&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528pathlib\u6a21\u5757<\/h3>\n<\/p>\n<p><h4>1\u3001\u83b7\u53d6\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55<\/h4>\n<\/p>\n<p><p>\u5728Python 3.4\u53ca\u4ee5\u4e0a\u7248\u672c\u4e2d\uff0c<code>pathlib<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u4e2a\u66f4\u9762\u5411\u5bf9\u8c61\u7684\u65b9\u5f0f\u6765\u5904\u7406\u6587\u4ef6\u548c\u76ee\u5f55\u3002\u8981\u83b7\u53d6\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\uff0c\u53ef\u4ee5\u4f7f\u7528<code>Path.cwd()<\/code>\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<p>current_path = Path.cwd()<\/p>\n<p>print(&quot;\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55:&quot;, current_path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u6539\u53d8\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55<\/h4>\n<\/p>\n<p><p>\u4e0eos\u6a21\u5757\u7c7b\u4f3c\uff0cpathlib\u6a21\u5757\u4e5f\u63d0\u4f9b\u4e86\u6539\u53d8\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u7684\u65b9\u6cd5\u3002\u867d\u7136\u6ca1\u6709\u76f4\u63a5\u7684\u65b9\u6cd5\u6765\u6539\u53d8\u5de5\u4f5c\u76ee\u5f55\uff0c\u4f46\u4f60\u53ef\u4ee5\u7ed3\u5408os\u6a21\u5757\u7684<code>os.chdir()<\/code>\u51fd\u6570\u6765\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002<\/p>\n<\/p>\n<p><p>\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<p>import os<\/p>\n<h2><strong>\u83b7\u53d6\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55<\/strong><\/h2>\n<p>current_path = Path.cwd()<\/p>\n<p>print(&quot;\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55:&quot;, current_path)<\/p>\n<h2><strong>\u6539\u53d8\u5de5\u4f5c\u76ee\u5f55<\/strong><\/h2>\n<p>new_path = Path(&quot;\/path\/to\/new\/directory&quot;)<\/p>\n<p>os.chdir(new_path)<\/p>\n<p>print(&quot;\u6539\u53d8\u540e\u7684\u5de5\u4f5c\u76ee\u5f55:&quot;, Path.cwd())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u521b\u5efa\u548c\u5220\u9664\u76ee\u5f55<\/h4>\n<\/p>\n<p><p>pathlib\u6a21\u5757\u63d0\u4f9b\u4e86\u66f4\u76f4\u89c2\u7684\u65b9\u6cd5\u6765\u521b\u5efa\u548c\u5220\u9664\u76ee\u5f55\u3002<code>Path.mkdir()<\/code>\u65b9\u6cd5\u7528\u4e8e\u521b\u5efa\u65b0\u76ee\u5f55\uff0c\u800c<code>Path.rmdir()<\/code>\u65b9\u6cd5\u7528\u4e8e\u5220\u9664\u76ee\u5f55\u3002<\/p>\n<\/p>\n<p><p>\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<h2><strong>\u521b\u5efa\u65b0\u76ee\u5f55<\/strong><\/h2>\n<p>new_directory = Path(&quot;new_directory&quot;)<\/p>\n<p>new_directory.mkdir()<\/p>\n<p>print(f&quot;\u76ee\u5f55 {new_directory} \u521b\u5efa\u6210\u529f&quot;)<\/p>\n<h2><strong>\u5220\u9664\u76ee\u5f55<\/strong><\/h2>\n<p>new_directory.rmdir()<\/p>\n<p>print(f&quot;\u76ee\u5f55 {new_directory} \u5220\u9664\u6210\u529f&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u533a\u5206\u76f8\u5bf9\u8def\u5f84\u548c\u7edd\u5bf9\u8def\u5f84<\/h3>\n<\/p>\n<p><h4>1\u3001\u76f8\u5bf9\u8def\u5f84<\/h4>\n<\/p>\n<p><p>\u76f8\u5bf9\u8def\u5f84\u662f\u76f8\u5bf9\u4e8e\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u7684\u8def\u5f84\u3002\u76f8\u5bf9\u8def\u5f84\u4e0d\u4ee5\u6839\u76ee\u5f55\uff08\u4f8b\u5982\uff0cUnix\u7cfb\u7edf\u4e2d\u7684<code>\/<\/code>\u6216Windows\u7cfb\u7edf\u4e2d\u7684\u76d8\u7b26\uff09\u5f00\u59cb\uff0c\u800c\u662f\u4ee5\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u4e3a\u57fa\u51c6\u6765\u89e3\u6790\u7684\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><p>\u793a\u4f8b\u4ee3\u7801\uff1a<\/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_path = os.getcwd()<\/p>\n<p>print(&quot;\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55:&quot;, current_path)<\/p>\n<h2><strong>\u4f7f\u7528\u76f8\u5bf9\u8def\u5f84<\/strong><\/h2>\n<p>relative_path = &quot;subdirectory\/file.txt&quot;<\/p>\n<p>absolute_path = os.path.join(current_path, relative_path)<\/p>\n<p>print(&quot;\u76f8\u5bf9\u8def\u5f84:&quot;, relative_path)<\/p>\n<p>print(&quot;\u5bf9\u5e94\u7684\u7edd\u5bf9\u8def\u5f84:&quot;, absolute_path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u7edd\u5bf9\u8def\u5f84<\/h4>\n<\/p>\n<p><p>\u7edd\u5bf9\u8def\u5f84\u662f\u4ece\u6839\u76ee\u5f55\u5f00\u59cb\u7684\u8def\u5f84\uff0c\u65e0\u8bba\u5f53\u524d\u7684\u5de5\u4f5c\u76ee\u5f55\u662f\u4ec0\u4e48\uff0c\u7edd\u5bf9\u8def\u5f84\u603b\u662f\u6307\u5411\u540c\u4e00\u4e2a\u4f4d\u7f6e\u3002\u7edd\u5bf9\u8def\u5f84\u901a\u5e38\u4ee5\u6839\u76ee\u5f55\u7b26\u53f7\uff08\u4f8b\u5982\uff0cUnix\u7cfb\u7edf\u4e2d\u7684<code>\/<\/code>\u6216Windows\u7cfb\u7edf\u4e2d\u7684\u76d8\u7b26\uff09\u5f00\u59cb\u3002<\/p>\n<\/p>\n<p><p>\u793a\u4f8b\u4ee3\u7801\uff1a<\/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_path = os.getcwd()<\/p>\n<p>print(&quot;\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55:&quot;, current_path)<\/p>\n<h2><strong>\u4f7f\u7528\u7edd\u5bf9\u8def\u5f84<\/strong><\/h2>\n<p>absolute_path = &quot;\/path\/to\/directory\/file.txt&quot;<\/p>\n<p>print(&quot;\u7edd\u5bf9\u8def\u5f84:&quot;, absolute_path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u8def\u5f84\u8f6c\u6362<\/h4>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6709\u65f6\u9700\u8981\u5728\u76f8\u5bf9\u8def\u5f84\u548c\u7edd\u5bf9\u8def\u5f84\u4e4b\u95f4\u8fdb\u884c\u8f6c\u6362\u3002os\u6a21\u5757\u548cpathlib\u6a21\u5757\u90fd\u63d0\u4f9b\u4e86\u65b9\u4fbf\u7684\u65b9\u6cd5\u6765\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002<\/p>\n<\/p>\n<p><p>\u793a\u4f8b\u4ee3\u7801\uff08\u4f7f\u7528os\u6a21\u5757\uff09\uff1a<\/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_path = os.getcwd()<\/p>\n<p>print(&quot;\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55:&quot;, current_path)<\/p>\n<h2><strong>\u5c06\u76f8\u5bf9\u8def\u5f84\u8f6c\u6362\u4e3a\u7edd\u5bf9\u8def\u5f84<\/strong><\/h2>\n<p>relative_path = &quot;subdirectory\/file.txt&quot;<\/p>\n<p>absolute_path = os.path.abspath(relative_path)<\/p>\n<p>print(&quot;\u76f8\u5bf9\u8def\u5f84:&quot;, relative_path)<\/p>\n<p>print(&quot;\u5bf9\u5e94\u7684\u7edd\u5bf9\u8def\u5f84:&quot;, absolute_path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u793a\u4f8b\u4ee3\u7801\uff08\u4f7f\u7528pathlib\u6a21\u5757\uff09\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<h2><strong>\u83b7\u53d6\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55<\/strong><\/h2>\n<p>current_path = Path.cwd()<\/p>\n<p>print(&quot;\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55:&quot;, current_path)<\/p>\n<h2><strong>\u5c06\u76f8\u5bf9\u8def\u5f84\u8f6c\u6362\u4e3a\u7edd\u5bf9\u8def\u5f84<\/strong><\/h2>\n<p>relative_path = Path(&quot;subdirectory\/file.txt&quot;)<\/p>\n<p>absolute_path = relative_path.resolve()<\/p>\n<p>print(&quot;\u76f8\u5bf9\u8def\u5f84:&quot;, relative_path)<\/p>\n<p>print(&quot;\u5bf9\u5e94\u7684\u7edd\u5bf9\u8def\u5f84:&quot;, absolute_path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u8def\u5f84\u64cd\u4f5c\u7684\u5b9e\u8df5\u5e94\u7528<\/h3>\n<\/p>\n<p><h4>1\u3001\u6587\u4ef6\u8bfb\u53d6\u548c\u5199\u5165<\/h4>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u7684\u7f16\u7a0b\u4e2d\uff0c\u6587\u4ef6\u8bfb\u53d6\u548c\u5199\u5165\u662f\u5e38\u89c1\u7684\u4efb\u52a1\u3002\u65e0\u8bba\u662f\u5904\u7406\u6587\u672c\u6587\u4ef6\u3001CSV\u6587\u4ef6\u8fd8\u662f\u5176\u4ed6\u7c7b\u578b\u7684\u6587\u4ef6\uff0c\u6b63\u786e\u5904\u7406\u8def\u5f84\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002<\/p>\n<\/p>\n<p><p>\u793a\u4f8b\u4ee3\u7801\uff08\u8bfb\u53d6\u6587\u4ef6\uff09\uff1a<\/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_path = os.getcwd()<\/p>\n<p>print(&quot;\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55:&quot;, current_path)<\/p>\n<h2><strong>\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9<\/strong><\/h2>\n<p>file_path = os.path.join(current_path, &quot;example.txt&quot;)<\/p>\n<p>with open(file_path, &#39;r&#39;) as file:<\/p>\n<p>    content = file.read()<\/p>\n<p>    print(&quot;\u6587\u4ef6\u5185\u5bb9:&quot;)<\/p>\n<p>    print(content)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u793a\u4f8b\u4ee3\u7801\uff08\u5199\u5165\u6587\u4ef6\uff09\uff1a<\/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_path = os.getcwd()<\/p>\n<p>print(&quot;\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55:&quot;, current_path)<\/p>\n<h2><strong>\u5199\u5165\u6587\u4ef6\u5185\u5bb9<\/strong><\/h2>\n<p>file_path = os.path.join(current_path, &quot;example.txt&quot;)<\/p>\n<p>with open(file_path, &#39;w&#39;) as file:<\/p>\n<p>    file.write(&quot;\u8fd9\u662f\u4e00\u4e2a\u793a\u4f8b\u6587\u4ef6\u5185\u5bb9&quot;)<\/p>\n<p>    print(&quot;\u6587\u4ef6\u5199\u5165\u6210\u529f&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u6279\u91cf\u5904\u7406\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u5728\u6570\u636e\u5904\u7406\u548c\u5206\u6790\u4e2d\uff0c\u5e38\u5e38\u9700\u8981\u6279\u91cf\u5904\u7406\u6587\u4ef6\u3002\u8fd9\u65f6\uff0c\u53ef\u4ee5\u7ed3\u5408os\u6a21\u5757\u548cpathlib\u6a21\u5757\u7684\u529f\u80fd\u6765\u5b9e\u73b0\u5bf9\u591a\u4e2a\u6587\u4ef6\u7684\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><p>\u793a\u4f8b\u4ee3\u7801\uff1a<\/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_path = os.getcwd()<\/p>\n<p>print(&quot;\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55:&quot;, current_path)<\/p>\n<h2><strong>\u6279\u91cf\u5904\u7406\u6587\u4ef6<\/strong><\/h2>\n<p>directory_path = os.path.join(current_path, &quot;data&quot;)<\/p>\n<p>for filename in os.listdir(directory_path):<\/p>\n<p>    file_path = os.path.join(directory_path, filename)<\/p>\n<p>    if os.path.isfile(file_path):<\/p>\n<p>        with open(file_path, &#39;r&#39;) as file:<\/p>\n<p>            content = file.read()<\/p>\n<p>            print(f&quot;\u6587\u4ef6 {filename} \u5185\u5bb9:&quot;)<\/p>\n<p>            print(content)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u8de8\u5e73\u53f0\u8def\u5f84\u64cd\u4f5c<\/h3>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528os.path\u6a21\u5757<\/h4>\n<\/p>\n<p><p>\u5728\u8de8\u5e73\u53f0\u5f00\u53d1\u4e2d\uff0c\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u7684\u8def\u5f84\u8868\u793a\u65b9\u5f0f\u6709\u6240\u4e0d\u540c\u3002os.path\u6a21\u5757\u63d0\u4f9b\u4e86\u8bb8\u591a\u8de8\u5e73\u53f0\u7684\u8def\u5f84\u64cd\u4f5c\u65b9\u6cd5\uff0c\u786e\u4fdd\u4ee3\u7801\u5728\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u4e0a\u90fd\u80fd\u6b63\u786e\u8fd0\u884c\u3002<\/p>\n<\/p>\n<p><p>\u793a\u4f8b\u4ee3\u7801\uff1a<\/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_path = os.getcwd()<\/p>\n<p>print(&quot;\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55:&quot;, current_path)<\/p>\n<h2><strong>\u8de8\u5e73\u53f0\u8def\u5f84\u62fc\u63a5<\/strong><\/h2>\n<p>subdirectory = &quot;subdirectory&quot;<\/p>\n<p>filename = &quot;file.txt&quot;<\/p>\n<p>file_path = os.path.join(current_path, subdirectory, filename)<\/p>\n<p>print(&quot;\u6587\u4ef6\u8def\u5f84:&quot;, file_path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528pathlib\u6a21\u5757<\/h4>\n<\/p>\n<p><p>pathlib\u6a21\u5757\u540c\u6837\u63d0\u4f9b\u4e86\u8de8\u5e73\u53f0\u7684\u8def\u5f84\u64cd\u4f5c\u65b9\u6cd5\uff0c\u5e76\u4e14\u66f4\u52a0\u9762\u5411\u5bf9\u8c61\u3002\u4f7f\u7528pathlib\u6a21\u5757\u53ef\u4ee5\u4f7f\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u548c\u6613\u8bfb\u3002<\/p>\n<\/p>\n<p><p>\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<h2><strong>\u83b7\u53d6\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55<\/strong><\/h2>\n<p>current_path = Path.cwd()<\/p>\n<p>print(&quot;\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55:&quot;, current_path)<\/p>\n<h2><strong>\u8de8\u5e73\u53f0\u8def\u5f84\u62fc\u63a5<\/strong><\/h2>\n<p>subdirectory = &quot;subdirectory&quot;<\/p>\n<p>filename = &quot;file.txt&quot;<\/p>\n<p>file_path = current_path \/ subdirectory \/ filename<\/p>\n<p>print(&quot;\u6587\u4ef6\u8def\u5f84:&quot;, file_path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u8def\u5f84\u64cd\u4f5c\u7684\u9ad8\u7ea7\u5e94\u7528<\/h3>\n<\/p>\n<p><h4>1\u3001\u8def\u5f84\u6a21\u5f0f\u5339\u914d<\/h4>\n<\/p>\n<p><p>\u5728\u5904\u7406\u6587\u4ef6\u548c\u76ee\u5f55\u65f6\uff0c\u6709\u65f6\u9700\u8981\u6839\u636e\u7279\u5b9a\u7684\u6a21\u5f0f\u5339\u914d\u6587\u4ef6\u3002os\u6a21\u5757\u63d0\u4f9b\u4e86glob\u6a21\u5757\u6765\u5b9e\u73b0\u6587\u4ef6\u7684\u6a21\u5f0f\u5339\u914d\uff0cpathlib\u6a21\u5757\u4e5f\u63d0\u4f9b\u4e86\u7c7b\u4f3c\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><p>\u793a\u4f8b\u4ee3\u7801\uff08\u4f7f\u7528glob\u6a21\u5757\uff09\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>import glob<\/p>\n<h2><strong>\u83b7\u53d6\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55<\/strong><\/h2>\n<p>current_path = os.getcwd()<\/p>\n<p>print(&quot;\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55:&quot;, current_path)<\/p>\n<h2><strong>\u5339\u914d\u7279\u5b9a\u6a21\u5f0f\u7684\u6587\u4ef6<\/strong><\/h2>\n<p>pattern = os.path.join(current_path, &quot;*.txt&quot;)<\/p>\n<p>for file_path in glob.glob(pattern):<\/p>\n<p>    print(&quot;\u5339\u914d\u5230\u7684\u6587\u4ef6:&quot;, file_path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u793a\u4f8b\u4ee3\u7801\uff08\u4f7f\u7528pathlib\u6a21\u5757\uff09\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<h2><strong>\u83b7\u53d6\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55<\/strong><\/h2>\n<p>current_path = Path.cwd()<\/p>\n<p>print(&quot;\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55:&quot;, current_path)<\/p>\n<h2><strong>\u5339\u914d\u7279\u5b9a\u6a21\u5f0f\u7684\u6587\u4ef6<\/strong><\/h2>\n<p>pattern = &quot;*.txt&quot;<\/p>\n<p>for file_path in current_path.glob(pattern):<\/p>\n<p>    print(&quot;\u5339\u914d\u5230\u7684\u6587\u4ef6:&quot;, file_path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u9012\u5f52\u904d\u5386\u76ee\u5f55<\/h4>\n<\/p>\n<p><p>\u6709\u65f6\u9700\u8981\u9012\u5f52\u5730\u904d\u5386\u76ee\u5f55\u4e2d\u7684\u6240\u6709\u6587\u4ef6\u548c\u5b50\u76ee\u5f55\u3002os\u6a21\u5757\u63d0\u4f9b\u4e86<code>os.walk()<\/code>\u51fd\u6570\uff0c\u800cpathlib\u6a21\u5757\u63d0\u4f9b\u4e86<code>Path.rglob()<\/code>\u65b9\u6cd5\u6765\u5b9e\u73b0\u9012\u5f52\u904d\u5386\u3002<\/p>\n<\/p>\n<p><p>\u793a\u4f8b\u4ee3\u7801\uff08\u4f7f\u7528os\u6a21\u5757\uff09\uff1a<\/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_path = os.getcwd()<\/p>\n<p>print(&quot;\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55:&quot;, current_path)<\/p>\n<h2><strong>\u9012\u5f52\u904d\u5386\u76ee\u5f55<\/strong><\/h2>\n<p>for root, dirs, files in os.walk(current_path):<\/p>\n<p>    for filename in files:<\/p>\n<p>        file_path = os.path.join(root, filename)<\/p>\n<p>        print(&quot;\u6587\u4ef6\u8def\u5f84:&quot;, file_path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u793a\u4f8b\u4ee3\u7801\uff08\u4f7f\u7528pathlib\u6a21\u5757\uff09\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<h2><strong>\u83b7\u53d6\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55<\/strong><\/h2>\n<p>current_path = Path.cwd()<\/p>\n<p>print(&quot;\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55:&quot;, current_path)<\/p>\n<h2><strong>\u9012\u5f52\u904d\u5386\u76ee\u5f55<\/strong><\/h2>\n<p>for file_path in current_path.rglob(&quot;*&quot;):<\/p>\n<p>    if file_path.is_file():<\/p>\n<p>        print(&quot;\u6587\u4ef6\u8def\u5f84:&quot;, file_path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u5904\u7406\u8def\u5f84\u4e2d\u7684\u7279\u6b8a\u5b57\u7b26<\/h3>\n<\/p>\n<p><h4>1\u3001\u8def\u5f84\u4e2d\u7684\u7a7a\u683c\u548c\u7279\u6b8a\u5b57\u7b26<\/h4>\n<\/p>\n<p><p>\u5728\u5904\u7406\u8def\u5f84\u65f6\uff0c\u6709\u65f6\u4f1a\u9047\u5230\u8def\u5f84\u4e2d\u5305\u542b\u7a7a\u683c\u6216\u5176\u4ed6\u7279\u6b8a\u5b57\u7b26\u7684\u60c5\u51b5\u3002\u8fd9\u65f6\u9700\u8981\u7279\u522b\u5c0f\u5fc3\uff0c\u786e\u4fdd\u4ee3\u7801\u80fd\u591f\u6b63\u786e\u5904\u7406\u8fd9\u4e9b\u7279\u6b8a\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><p>\u793a\u4f8b\u4ee3\u7801\uff08\u5904\u7406\u8def\u5f84\u4e2d\u7684\u7a7a\u683c\uff09\uff1a<\/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_path = os.getcwd()<\/p>\n<p>print(&quot;\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55:&quot;, current_path)<\/p>\n<h2><strong>\u5305\u542b\u7a7a\u683c\u7684\u8def\u5f84<\/strong><\/h2>\n<p>directory_with_spaces = &quot;directory with spaces&quot;<\/p>\n<p>file_with_spaces = &quot;file with spaces.txt&quot;<\/p>\n<p>file_path = os.path.join(current_path, directory_with_spaces, file_with_spaces)<\/p>\n<p>print(&quot;\u5305\u542b\u7a7a\u683c\u7684\u6587\u4ef6\u8def\u5f84:&quot;, file_path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u8def\u5f84\u4e2d\u7684Unicode\u5b57\u7b26<\/h4>\n<\/p>\n<p><p>\u5728\u56fd\u9645\u5316\u5e94\u7528\u4e2d\uff0c\u8def\u5f84\u4e2d\u53ef\u80fd\u5305\u542bUnicode\u5b57\u7b26\u3002Python\u7684os\u6a21\u5757\u548cpathlib\u6a21\u5757\u90fd\u80fd\u591f\u6b63\u786e\u5904\u7406\u8def\u5f84\u4e2d\u7684Unicode\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><p>\u793a\u4f8b\u4ee3\u7801\uff1a<\/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_path = os.getcwd()<\/p>\n<p>print(&quot;\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55:&quot;, current_path)<\/p>\n<h2><strong>\u5305\u542bUnicode\u5b57\u7b26\u7684\u8def\u5f84<\/strong><\/h2>\n<p>directory_with_unicode = &quot;\u76ee\u5f55&quot;<\/p>\n<p>file_with_unicode = &quot;\u6587\u4ef6.txt&quot;<\/p>\n<p>file_path = os.path.join(current_path, directory_with_unicode, file_with_unicode)<\/p>\n<p>print(&quot;\u5305\u542bUnicode\u5b57\u7b26\u7684\u6587\u4ef6\u8def\u5f84:&quot;, file_path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001\u8def\u5f84\u64cd\u4f5c\u7684\u6027\u80fd\u4f18\u5316<\/h3>\n<\/p>\n<p><h4>1\u3001\u51cf\u5c11\u8def\u5f84\u64cd\u4f5c\u7684\u6b21\u6570<\/h4>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u91cf\u6587\u4ef6\u65f6\uff0c\u9891\u7e41\u7684\u8def\u5f84\u64cd\u4f5c\u53ef\u80fd\u4f1a\u5f71\u54cd\u6027\u80fd\u3002\u53ef\u4ee5\u901a\u8fc7\u51cf\u5c11\u8def\u5f84\u64cd\u4f5c\u7684\u6b21\u6570\u6765\u4f18\u5316\u6027\u80fd\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5c06\u8def\u5f84\u62fc\u63a5\u64cd\u4f5c\u653e\u5728\u5faa\u73af\u5916\u90e8\uff0c\u800c\u4e0d\u662f\u5728\u6bcf\u6b21\u8fed\u4ee3\u4e2d\u8fdb\u884c\u8def\u5f84\u62fc\u63a5\u3002<\/p>\n<\/p>\n<p><p>\u793a\u4f8b\u4ee3\u7801\uff1a<\/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_path = os.getcwd()<\/p>\n<p>print(&quot;\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55:&quot;, current_path)<\/p>\n<h2><strong>\u51cf\u5c11\u8def\u5f84\u64cd\u4f5c\u7684\u6b21\u6570<\/strong><\/h2>\n<p>directory_path = os.path.join(current_path, &quot;data&quot;)<\/p>\n<p>for filename in os.listdir(directory_path):<\/p>\n<p>    file_path = os.path.join(directory_path, filename)<\/p>\n<p>    if os.path.isfile(file_path):<\/p>\n<p>        with open(file_path, &#39;r&#39;) as file:<\/p>\n<p>            content = file.read()<\/p>\n<p>            print(f&quot;\u6587\u4ef6 {filename} \u5185\u5bb9:&quot;)<\/p>\n<p>            print(content)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528\u7f13\u5b58<\/h4>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u91cf\u6587\u4ef6\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u7f13\u5b58\u6765\u63d0\u9ad8\u6027\u80fd\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5c06\u5df2\u7ecf\u8ba1\u7b97\u8fc7\u7684\u8def\u5f84\u5b58\u50a8\u5728\u4e00\u4e2a\u5b57\u5178\u4e2d\uff0c\u4ee5\u4fbf\u5728\u9700\u8981\u65f6\u5feb\u901f\u67e5\u627e\u3002<\/p>\n<\/p>\n<p><p>\u793a\u4f8b\u4ee3\u7801\uff1a<\/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_path = os.getcwd()<\/p>\n<p>print(&quot;\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55:&quot;, current_path)<\/p>\n<h2><strong>\u4f7f\u7528\u7f13\u5b58<\/strong><\/h2>\n<p>path_cache = {}<\/p>\n<p>directory_path = os.path.join(current_path, &quot;data&quot;)<\/p>\n<p>for filename in os.listdir(directory_path):<\/p>\n<p>    if filename not in path_cache:<\/p>\n<p>        path_cache[filename] = os.path.join(directory_path, filename)<\/p>\n<p>    file_path = path_cache[filename]<\/p>\n<p>    if os.path.isfile(file_path):<\/p>\n<p>        with open(file_path, &#39;r&#39;) as file:<\/p>\n<p>            content = file.read()<\/p>\n<p>            print(f&quot;\u6587\u4ef6 {filename} \u5185\u5bb9:&quot;)<\/p>\n<p>            print(content)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e5d\u3001\u9519\u8bef\u5904\u7406\u548c\u5f02\u5e38\u6355\u83b7<\/h3>\n<\/p>\n<p><h4>1\u3001\u5904\u7406\u6587\u4ef6\u548c\u76ee\u5f55\u4e0d\u5b58\u5728\u7684\u60c5\u51b5<\/h4>\n<\/p>\n<p><p>\u5728\u8fdb\u884c\u8def\u5f84\u64cd\u4f5c\u65f6\uff0c\u6587\u4ef6\u6216\u76ee\u5f55\u53ef\u80fd\u4e0d\u5b58\u5728\u3002\u8fd9\u65f6\u9700\u8981\u8fdb\u884c\u9519\u8bef\u5904\u7406\uff0c\u6355\u83b7\u76f8\u5e94\u7684\u5f02\u5e38\u3002os\u6a21\u5757\u548cpathlib\u6a21\u5757\u90fd\u63d0\u4f9b\u4e86\u76f8\u5e94\u7684\u5f02\u5e38\u7c7b\u6765\u5904\u7406\u8fd9\u4e9b\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><p>\u793a\u4f8b\u4ee3\u7801\uff08\u4f7f\u7528os\u6a21\u5757\uff09\uff1a<\/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_path = os.getcwd()<\/p>\n<p>print(&quot;\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55:&quot;, current_path)<\/p>\n<h2><strong>\u5904\u7406\u6587\u4ef6\u4e0d\u5b58\u5728\u7684\u60c5\u51b5<\/strong><\/h2>\n<p>file_path = os.path.join(current_path, &quot;nonexistent_file.txt&quot;)<\/p>\n<p>try:<\/p>\n<p>    with open(file_path, &#39;r&#39;) as file:<\/p>\n<p>        content = file.read()<\/p>\n<p>        print(&quot;\u6587\u4ef6\u5185\u5bb9:&quot;)<\/p>\n<p>        print(content)<\/p>\n<p>except FileNotFoundError:<\/p>\n<p>    print(f&quot;\u6587\u4ef6 {file_path} \u4e0d\u5b58\u5728&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u793a\u4f8b\u4ee3\u7801\uff08\u4f7f\u7528pathlib\u6a21\u5757\uff09\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<h2><strong>\u83b7\u53d6\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55<\/strong><\/h2>\n<p>current_path = Path.cwd()<\/p>\n<p>print(&quot;\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55:&quot;, current_path)<\/p>\n<h2><strong>\u5904\u7406\u6587\u4ef6\u4e0d\u5b58\u5728\u7684\u60c5\u51b5<\/strong><\/h2>\n<p>file_path = current_path \/ &quot;nonexistent_file.txt&quot;<\/p>\n<p>try:<\/p>\n<p>    content = file_path.read_text()<\/p>\n<p>    print(&quot;\u6587\u4ef6\u5185\u5bb9:&quot;)<\/p>\n<p>    print(content)<\/p>\n<p>except FileNotFoundError:<\/p>\n<p>    print(f&quot;\u6587\u4ef6 {file_path} \u4e0d\u5b58\u5728&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u5904\u7406\u6743\u9650\u9519\u8bef<\/h4>\n<\/p>\n<p><p>\u5728\u8fdb\u884c\u6587\u4ef6\u548c\u76ee\u5f55\u64cd\u4f5c\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u6743\u9650\u9519\u8bef\u3002\u8fd9\u65f6\u9700\u8981\u6355\u83b7\u76f8\u5e94\u7684\u5f02\u5e38\uff0c\u5e76\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<\/p>\n<p><p>\u793a\u4f8b\u4ee3\u7801\uff08\u4f7f\u7528os\u6a21\u5757\uff09\uff1a<\/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_path = os.getcwd()<\/p>\n<p>print(&quot;\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55:&quot;, current_path)<\/p>\n<h2><strong>\u5904\u7406\u6743\u9650\u9519\u8bef<\/strong><\/h2>\n<p>file_path = os.path.join(current_path, &quot;restricted_file.txt&quot;)<\/p>\n<p>try:<\/p>\n<p>    with open(file_path, &#39;r&#39;) as file:<\/p>\n<p>        content = file.read()<\/p>\n<p>        print(&quot;\u6587\u4ef6\u5185\u5bb9:&quot;)<\/p>\n<p>        print(content)<\/p>\n<p>except PermissionError:<\/p>\n<p>    print(f&quot;\u6ca1\u6709\u6743\u9650\u8bbf\u95ee\u6587\u4ef6 {file_path}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u793a\u4f8b\u4ee3\u7801\uff08\u4f7f\u7528pathlib\u6a21\u5757\uff09\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<h2><strong>\u83b7\u53d6\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55<\/strong><\/h2>\n<p>current_path = Path.cwd()<\/p>\n<p>print(&quot;\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55:&quot;, current_path)<\/p>\n<h2><strong>\u5904\u7406\u6743\u9650\u9519\u8bef<\/strong><\/h2>\n<p>file_path = current_path \/ &quot;restricted_file.txt&quot;<\/p>\n<p>try:<\/p>\n<p>    content = file_path.read_text()<\/p>\n<p>    print(&quot;\u6587\u4ef6\u5185\u5bb9:&quot;)<\/p>\n<p>    print(content)<\/p>\n<p>except PermissionError:<\/p>\n<p>    print(f&quot;\u6ca1\u6709\u6743\u9650\u8bbf\u95ee\u6587\u4ef6 {file_path}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u672c\u6587\u7684\u5b66\u4e60\uff0c\u6211\u4eec\u8be6\u7ec6\u4ecb\u7ecd\u4e86\u5982\u4f55\u5728Python\u4e2d\u67e5\u770b\u5f53\u524d\u8def\u5f84\uff0c\u5e76\u5bf9\u8def\u5f84\u7684\u64cd\u4f5c\u8fdb\u884c\u4e86\u6df1\u5165\u63a2\u8ba8\u3002\u6211\u4eec\u4ece\u4f7f\u7528os\u6a21\u5757\u3001pathlib\u6a21\u5757\u51fa\u53d1\uff0c\u4ecb\u7ecd\u4e86\u83b7\u53d6\u548c\u6539\u53d8\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u3001\u521b\u5efa\u548c\u5220\u9664\u76ee\u5f55\u3001\u533a\u5206\u76f8\u5bf9\u8def\u5f84\u548c\u7edd\u5bf9\u8def\u5f84\u3001\u6587\u4ef6\u8bfb\u53d6\u548c\u5199\u5165\u3001\u6279\u91cf\u5904\u7406\u6587\u4ef6\u3001\u8de8\u5e73\u53f0\u8def\u5f84\u64cd\u4f5c\u3001\u8def\u5f84\u6a21\u5f0f\u5339\u914d\u3001\u9012\u5f52\u904d\u5386\u76ee\u5f55\u3001\u5904\u7406\u8def\u5f84\u4e2d\u7684\u7279\u6b8a\u5b57\u7b26\u3001\u8def\u5f84\u64cd\u4f5c\u7684\u6027\u80fd\u4f18\u5316\u4ee5\u53ca\u9519\u8bef\u5904\u7406\u548c\u5f02\u5e38\u6355\u83b7\u7b49\u5404\u4e2a\u65b9\u9762\u7684\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><p><strong>\u638c\u63e1\u8fd9\u4e9b\u8def\u5f84\u64cd\u4f5c\u7684\u65b9\u6cd5\u548c\u6280\u5de7\uff0c\u53ef\u4ee5\u5e2e\u52a9\u4f60\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\u66f4\u9ad8\u6548\u5730\u5904\u7406\u6587\u4ef6\u548c\u76ee\u5f55\uff0c\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u7ef4\u62a4\u6027\u3002\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff0c\u795d\u4f60\u5728Python\u7f16\u7a0b\u7684\u9053\u8def\u4e0a\u4e0d\u65ad\u8fdb\u6b65\uff01<\/strong><\/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 \/>\u60a8\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_path = os.getcwd()\nprint(&quot;\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u662f:&quot;, current_path)\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u5c06\u8fd4\u56de\u60a8\u5f53\u524d\u8fd0\u884cPython\u811a\u672c\u7684\u76ee\u5f55\u8def\u5f84\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u6539\u53d8\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\uff1f<\/strong><br \/>\u8981\u6539\u53d8\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\uff0c\u53ef\u4ee5\u4f7f\u7528<code>os<\/code>\u6a21\u5757\u4e2d\u7684<code>chdir()<\/code>\u51fd\u6570\u3002\u60a8\u53ea\u9700\u4f20\u5165\u65b0\u7684\u8def\u5f84\u4f5c\u4e3a\u53c2\u6570\u3002\u793a\u4f8b\u4ee3\u7801\uff1a  <\/p>\n<pre><code class=\"language-python\">import os\nos.chdir(&#39;\/\u65b0\u8def\u5f84\/\u76ee\u5f55&#39;)\nprint(&quot;\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u5df2\u66f4\u6539\u4e3a:&quot;, os.getcwd())\n<\/code><\/pre>\n<p>\u8bf7\u786e\u4fdd\u63d0\u4f9b\u7684\u8def\u5f84\u662f\u6709\u6548\u7684\uff0c\u8fd9\u6837\u624d\u80fd\u987a\u5229\u66f4\u6539\u76ee\u5f55\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u67e5\u770b\u5f53\u524d\u8def\u5f84\u7684\u6587\u4ef6\u548c\u6587\u4ef6\u5939\uff1f<\/strong><br \/>\u60a8\u53ef\u4ee5\u4f7f\u7528<code>os<\/code>\u6a21\u5757\u7684<code>listdir()<\/code>\u51fd\u6570\u67e5\u770b\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u4e0b\u7684\u6240\u6709\u6587\u4ef6\u548c\u6587\u4ef6\u5939\u3002\u4ee5\u4e0b\u662f\u793a\u4f8b\u4ee3\u7801\uff1a  <\/p>\n<pre><code class=\"language-python\">import os\nfiles_and_folders = os.listdir(os.getcwd())\nprint(&quot;\u5f53\u524d\u76ee\u5f55\u4e0b\u7684\u6587\u4ef6\u548c\u6587\u4ef6\u5939\u6709:&quot;, files_and_folders)\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u5c06\u5217\u51fa\u5f53\u524d\u76ee\u5f55\u4e2d\u7684\u6240\u6709\u5185\u5bb9\uff0c\u4fbf\u4e8e\u60a8\u8fdb\u4e00\u6b65\u64cd\u4f5c\u6216\u67e5\u627e\u6587\u4ef6\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4f7f\u7528os\u6a21\u5757\u3001\u4f7f\u7528pathlib\u6a21\u5757\u3001\u533a\u5206\u76f8\u5bf9\u8def\u5f84\u548c\u7edd\u5bf9\u8def\u5f84\u3002\u5176\u4e2d\uff0c\u4f7f\u7528os\u6a21\u5757\u662f\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\u3002 os\u6a21\u5757\u662fP [&hellip;]","protected":false},"author":3,"featured_media":1182729,"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\/1182726"}],"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=1182726"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1182726\/revisions"}],"predecessor-version":[{"id":1182731,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1182726\/revisions\/1182731"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1182729"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1182726"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1182726"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1182726"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}