{"id":953474,"date":"2024-12-27T01:44:05","date_gmt":"2024-12-26T17:44:05","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/953474.html"},"modified":"2024-12-27T01:44:07","modified_gmt":"2024-12-26T17:44:07","slug":"%e5%a6%82%e4%bd%95%e6%9f%a5%e7%9c%8b%e5%bd%93%e5%89%8d%e7%9b%ae%e5%bd%95-python","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/953474.html","title":{"rendered":"\u5982\u4f55\u67e5\u770b\u5f53\u524d\u76ee\u5f55 python"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25090935\/0b2d2516-ff5b-430d-a5bc-e88206eac5ac.webp\" alt=\"\u5982\u4f55\u67e5\u770b\u5f53\u524d\u76ee\u5f55 python\" \/><\/p>\n<p><p> <strong>\u8981\u67e5\u770b\u5f53\u524d\u76ee\u5f55\u7684Python\u4ee3\u7801\uff0c\u901a\u5e38\u4f7f\u7528<code>os<\/code>\u6a21\u5757\u548c<code>os.getcwd()<\/code>\u51fd\u6570\u3001<code>pathlib<\/code>\u6a21\u5757\u548c<code>Path.cwd()<\/code>\u65b9\u6cd5\u3002\u5bf9\u4e8e\u66f4\u590d\u6742\u7684\u8def\u5f84\u64cd\u4f5c\uff0c<code>os<\/code>\u6a21\u5757\u8fd8\u63d0\u4f9b\u4e86\u5176\u4ed6\u6709\u7528\u7684\u51fd\u6570\u3002<\/strong>\u5176\u4e2d\uff0c<code>os.getcwd()<\/code>\u51fd\u6570\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\uff0c\u5b83\u53ef\u4ee5\u8fd4\u56de\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u7684\u7edd\u5bf9\u8def\u5f84\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e24\u79cd\u65b9\u6cd5\u4ee5\u53ca\u5b83\u4eec\u7684\u4f18\u7f3a\u70b9\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528OS\u6a21\u5757<\/p>\n<\/p>\n<p><p><code>os<\/code>\u6a21\u5757\u662fPython\u6807\u51c6\u5e93\u4e2d\u7684\u4e00\u4e2a\u6a21\u5757\uff0c\u63d0\u4f9b\u4e86\u8bb8\u591a\u4e0e\u64cd\u4f5c\u7cfb\u7edf\u4ea4\u4e92\u7684\u51fd\u6570\u3002<code>os.getcwd()<\/code>\u51fd\u6570\u662f\u7528\u6765\u83b7\u53d6\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u7684\u6700\u76f4\u63a5\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>1.1\u3001\u83b7\u53d6\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55<\/p>\n<\/p>\n<p><p>\u8981\u83b7\u53d6\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\uff0c\u4f60\u53ea\u9700\u5bfc\u5165<code>os<\/code>\u6a21\u5757\u5e76\u8c03\u7528<code>os.getcwd()<\/code>\u51fd\u6570\u3002\u8fd9\u4e2a\u51fd\u6570\u4f1a\u8fd4\u56de\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u8868\u793a\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u7684\u7edd\u5bf9\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>current_directory = os.getcwd()<\/p>\n<p>print(&quot;Current Directory:&quot;, current_directory)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u7b80\u5355\u7684\u4ee3\u7801\u7247\u6bb5\u4f1a\u6253\u5370\u51fa\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u7684\u8def\u5f84\u3002\u5728\u4f7f\u7528Python\u811a\u672c\u8fdb\u884c\u6587\u4ef6\u64cd\u4f5c\u65f6\uff0c\u4e86\u89e3\u5f53\u524d\u7684\u5de5\u4f5c\u76ee\u5f55\u662f\u81f3\u5173\u91cd\u8981\u7684\uff0c\u56e0\u4e3a\u5b83\u5f71\u54cd\u5230\u6587\u4ef6\u7684\u8bfb\u5199\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><p>1.2\u3001\u6539\u53d8\u5de5\u4f5c\u76ee\u5f55<\/p>\n<\/p>\n<p><p>\u6709\u65f6\u5019\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u6539\u53d8\u5de5\u4f5c\u76ee\u5f55\u3002\u53ef\u4ee5\u4f7f\u7528<code>os.chdir(path)<\/code>\u51fd\u6570\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">os.chdir(&#39;\/path\/to\/new\/directory&#39;)<\/p>\n<p>print(&quot;Changed Directory:&quot;, os.getcwd())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u4f60\u53ef\u4ee5\u5728\u8fd0\u884c\u65f6\u66f4\u6539\u811a\u672c\u7684\u5de5\u4f5c\u76ee\u5f55\uff0c\u4ee5\u4fbf\u5728\u4e0d\u540c\u7684\u6587\u4ef6\u8def\u5f84\u4e0b\u8fdb\u884c\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528PATHLIB\u6a21\u5757<\/p>\n<\/p>\n<p><p><code>pathlib<\/code>\u662fPython 3.4\u5f15\u5165\u7684\u4e00\u4e2a\u6a21\u5757\uff0c\u7528\u4e8e\u5904\u7406\u6587\u4ef6\u548c\u76ee\u5f55\u8def\u5f84\u3002\u5b83\u63d0\u4f9b\u4e86\u9762\u5411\u5bf9\u8c61\u7684\u8def\u5f84\u64cd\u4f5c\u65b9\u5f0f\uff0c\u66f4\u52a0\u76f4\u89c2\u548c\u6613\u4e8e\u7406\u89e3\u3002<\/p>\n<\/p>\n<p><p>2.1\u3001\u83b7\u53d6\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55<\/p>\n<\/p>\n<p><p>\u4f7f\u7528<code>pathlib<\/code>\u6a21\u5757\u83b7\u53d6\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u7684\u8def\u5f84\u5bf9\u8c61\uff0c\u53ef\u4ee5\u8c03\u7528<code>Path.cwd()<\/code>\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<p>current_directory = Path.cwd()<\/p>\n<p>print(&quot;Current Directory:&quot;, current_directory)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0e<code>os.getcwd()<\/code>\u4e0d\u540c\u7684\u662f\uff0c<code>Path.cwd()<\/code>\u8fd4\u56de\u7684\u662f\u4e00\u4e2a<code>Path<\/code>\u5bf9\u8c61\uff0c\u800c\u4e0d\u662f\u5b57\u7b26\u4e32\u3002\u8fd9\u4f7f\u5f97\u540e\u7eed\u7684\u8def\u5f84\u64cd\u4f5c\u66f4\u4e3a\u4fbf\u6377\u548c\u7075\u6d3b\u3002<\/p>\n<\/p>\n<p><p>2.2\u3001\u8def\u5f84\u64cd\u4f5c<\/p>\n<\/p>\n<p><p><code>Path<\/code>\u5bf9\u8c61\u652f\u6301\u591a\u79cd\u8def\u5f84\u64cd\u4f5c\uff0c\u4f8b\u5982\u62fc\u63a5\u8def\u5f84\u3001\u5224\u65ad\u8def\u5f84\u662f\u5426\u5b58\u5728\u3001\u521b\u5efa\u76ee\u5f55\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u62fc\u63a5\u8def\u5f84<\/p>\n<p>new_path = current_directory \/ &#39;new_folder&#39;<\/p>\n<p>print(&quot;New Path:&quot;, new_path)<\/p>\n<h2><strong>\u5224\u65ad\u8def\u5f84\u662f\u5426\u5b58\u5728<\/strong><\/h2>\n<p>if not new_path.exists():<\/p>\n<p>    new_path.mkdir()<\/p>\n<p>    print(&quot;Directory created:&quot;, new_path)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;Directory already exists:&quot;, new_path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u603b\u7ed3\uff1a<\/strong><code>os<\/code>\u6a21\u5757\u548c<code>pathlib<\/code>\u6a21\u5757\u90fd\u53ef\u4ee5\u7528\u6765\u83b7\u53d6\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u3002<code>os<\/code>\u6a21\u5757\u7684<code>os.getcwd()<\/code>\u51fd\u6570\u7b80\u5355\u76f4\u63a5\uff0c\u800c<code>pathlib<\/code>\u6a21\u5757\u7684<code>Path.cwd()<\/code>\u65b9\u6cd5\u63d0\u4f9b\u4e86\u66f4\u9762\u5411\u5bf9\u8c61\u7684\u8def\u5f84\u64cd\u4f5c\u65b9\u5f0f\u3002\u6839\u636e\u9700\u6c42\u9009\u62e9\u9002\u5408\u7684\u65b9\u6cd5\u6765\u5904\u7406\u8def\u5f84\u64cd\u4f5c\uff0c\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001OS\u6a21\u5757\u7684\u5176\u4ed6\u529f\u80fd<\/p>\n<\/p>\n<p><p>\u9664\u4e86\u83b7\u53d6\u5f53\u524d\u76ee\u5f55\uff0c<code>os<\/code>\u6a21\u5757\u8fd8\u63d0\u4f9b\u4e86\u8bb8\u591a\u5176\u4ed6\u5b9e\u7528\u7684\u529f\u80fd\uff0c\u7528\u4e8e\u6587\u4ef6\u548c\u76ee\u5f55\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><p>3.1\u3001\u5217\u51fa\u76ee\u5f55\u5185\u5bb9<\/p>\n<\/p>\n<p><p>\u4f7f\u7528<code>os.listdir(path)<\/code>\u51fd\u6570\u53ef\u4ee5\u5217\u51fa\u6307\u5b9a\u8def\u5f84\u4e0b\u7684\u6240\u6709\u6587\u4ef6\u548c\u76ee\u5f55\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">files_and_directories = os.listdir(current_directory)<\/p>\n<p>print(&quot;Files and Directories:&quot;, files_and_directories)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5bf9\u4e8e\u9700\u8981\u904d\u5386\u76ee\u5f55\u4e0b\u7684\u6240\u6709\u6587\u4ef6\u8fdb\u884c\u6279\u91cf\u64cd\u4f5c\u7684\u573a\u666f\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><p>3.2\u3001\u6587\u4ef6\u548c\u76ee\u5f55\u7684\u68c0\u67e5<\/p>\n<\/p>\n<p><p><code>os<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u4e9b\u51fd\u6570\u7528\u4e8e\u68c0\u67e5\u6587\u4ef6\u548c\u76ee\u5f55\u7684\u5c5e\u6027\uff0c\u4f8b\u5982<code>os.path.isfile(path)<\/code>\u548c<code>os.path.isdir(path)<\/code>\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for item in files_and_directories:<\/p>\n<p>    item_path = os.path.join(current_directory, item)<\/p>\n<p>    if os.path.isfile(item_path):<\/p>\n<p>        print(f&quot;{item} is a file.&quot;)<\/p>\n<p>    elif os.path.isdir(item_path):<\/p>\n<p>        print(f&quot;{item} is a directory.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u4e9b\u51fd\u6570\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u5224\u65ad\u8def\u5f84\u662f\u6587\u4ef6\u8fd8\u662f\u76ee\u5f55\uff0c\u4ee5\u4fbf\u8fdb\u884c\u4e0d\u540c\u7684\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001PATHLIB\u6a21\u5757\u7684\u9ad8\u7ea7\u7528\u6cd5<\/p>\n<\/p>\n<p><p><code>pathlib<\/code>\u6a21\u5757\u4e0d\u4ec5\u63d0\u4f9b\u4e86\u7b80\u5355\u7684\u8def\u5f84\u64cd\u4f5c\uff0c\u8fd8\u652f\u6301\u66f4\u9ad8\u7ea7\u7684\u6587\u4ef6\u548c\u76ee\u5f55\u7ba1\u7406\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><p>4.1\u3001\u904d\u5386\u76ee\u5f55\u6811<\/p>\n<\/p>\n<p><p>\u4f7f\u7528<code>Path<\/code>\u5bf9\u8c61\u7684<code>rglob()<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u9012\u5f52\u5730\u904d\u5386\u76ee\u5f55\u6811\u4e2d\u7684\u6240\u6709\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for file_path in current_directory.rglob(&#39;*.py&#39;):<\/p>\n<p>    print(&quot;Python file found:&quot;, file_path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u9002\u5408\u67e5\u627e\u7279\u5b9a\u7c7b\u578b\u7684\u6587\u4ef6\uff0c\u4f8b\u5982\u5728\u9879\u76ee\u4e2d\u67e5\u627e\u6240\u6709\u7684Python\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><p>4.2\u3001\u6587\u4ef6\u8bfb\u5199<\/p>\n<\/p>\n<p><p><code>pathlib<\/code>\u6a21\u5757\u4e2d\u7684<code>Path<\/code>\u5bf9\u8c61\u8fd8\u652f\u6301\u6587\u4ef6\u7684\u8bfb\u5199\u64cd\u4f5c\uff0c\u63d0\u4f9b\u4e86\u4e00\u79cd\u66f4\u4e3a\u4f18\u96c5\u7684\u65b9\u5f0f\u6765\u5904\u7406\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">file_path = current_directory \/ &#39;example.txt&#39;<\/p>\n<h2><strong>\u5199\u5165\u6587\u4ef6<\/strong><\/h2>\n<p>file_path.write_text(&quot;Hello, World!&quot;)<\/p>\n<h2><strong>\u8bfb\u53d6\u6587\u4ef6<\/strong><\/h2>\n<p>content = file_path.read_text()<\/p>\n<p>print(&quot;File Content:&quot;, content)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u907f\u514d\u4f7f\u7528\u4f20\u7edf\u7684<code>open()<\/code>\u51fd\u6570\uff0c\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u660e\u4e86\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u6700\u4f73\u5b9e\u8df5<\/p>\n<\/p>\n<p><p>\u6839\u636e\u4e0d\u540c\u7684\u9700\u6c42\uff0c\u9009\u62e9\u9002\u5408\u7684\u6a21\u5757\u548c\u65b9\u6cd5\u6765\u5904\u7406\u8def\u5f84\u64cd\u4f5c\u662f\u7f16\u5199\u9ad8\u6548Python\u4ee3\u7801\u7684\u5173\u952e\u3002<\/p>\n<\/p>\n<p><p>5.1\u3001\u4f7f\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668<\/p>\n<\/p>\n<p><p>\u5728\u8fdb\u884c\u6587\u4ef6\u64cd\u4f5c\u65f6\uff0c\u4f7f\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\uff08\u5373<code>with<\/code>\u8bed\u53e5\uff09\u53ef\u4ee5\u786e\u4fdd\u6587\u4ef6\u5728\u64cd\u4f5c\u5b8c\u6210\u540e\u81ea\u52a8\u5173\u95ed\uff0c\u907f\u514d\u8d44\u6e90\u6cc4\u6f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;example.txt&#39;, &#39;w&#39;) as file:<\/p>\n<p>    file.write(&quot;Hello, World!&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>5.2\u3001\u9519\u8bef\u5904\u7406<\/p>\n<\/p>\n<p><p>\u5728\u8fdb\u884c\u6587\u4ef6\u548c\u76ee\u5f55\u64cd\u4f5c\u65f6\uff0c\u52a1\u5fc5\u8fdb\u884c\u9519\u8bef\u5904\u7406\uff0c\u4ee5\u786e\u4fdd\u7a0b\u5e8f\u7684\u5065\u58ee\u6027\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>try<\/code>&#8230;<code>except<\/code>\u8bed\u53e5\u6765\u6355\u83b7\u53ef\u80fd\u51fa\u73b0\u7684\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">try:<\/p>\n<p>    os.chdir(&#39;\/invalid\/path&#39;)<\/p>\n<p>except FileNotFoundError as e:<\/p>\n<p>    print(&quot;Error:&quot;, e)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u4e9b\u5b9e\u8df5\uff0c\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u5065\u58ee\u6027\u548c\u53ef\u7ef4\u62a4\u6027\uff0c\u51cf\u5c11\u6f5c\u5728\u7684bug\u548c\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><p>\u516d\u3001\u8de8\u5e73\u53f0\u517c\u5bb9\u6027<\/p>\n<\/p>\n<p><p>\u5728\u5904\u7406\u8def\u5f84\u65f6\uff0c\u8de8\u5e73\u53f0\u7684\u517c\u5bb9\u6027\u662f\u4e00\u4e2a\u91cd\u8981\u7684\u8003\u8651\u56e0\u7d20\u3002\u7531\u4e8e\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u7684\u6587\u4ef6\u8def\u5f84\u8868\u793a\u65b9\u5f0f\u4e0d\u540c\uff0c\u56e0\u6b64\u5728\u7f16\u5199\u4ee3\u7801\u65f6\u9700\u8981\u6ce8\u610f\u8fd9\u4e9b\u5dee\u5f02\u3002<\/p>\n<\/p>\n<p><p>6.1\u3001\u4f7f\u7528OS\u6a21\u5757\u7684\u8def\u5f84\u62fc\u63a5<\/p>\n<\/p>\n<p><p>\u5728\u4f7f\u7528<code>os<\/code>\u6a21\u5757\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7<code>os.path.join()<\/code>\u51fd\u6570\u6765\u62fc\u63a5\u8def\u5f84\uff0c\u4ee5\u786e\u4fdd\u8def\u5f84\u7684\u8de8\u5e73\u53f0\u517c\u5bb9\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">path = os.path.join(&#39;folder&#39;, &#39;subfolder&#39;, &#39;file.txt&#39;)<\/p>\n<p>print(&quot;Joined Path:&quot;, path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u4f1a\u6839\u636e\u64cd\u4f5c\u7cfb\u7edf\u7684\u8def\u5f84\u5206\u9694\u7b26\u81ea\u52a8\u62fc\u63a5\u8def\u5f84\uff0c\u786e\u4fdd\u4ee3\u7801\u5728\u4e0d\u540c\u5e73\u53f0\u4e0a\u90fd\u80fd\u6b63\u5e38\u8fd0\u884c\u3002<\/p>\n<\/p>\n<p><p>6.2\u3001\u4f7f\u7528PATHLIB\u6a21\u5757<\/p>\n<\/p>\n<p><p><code>pathlib<\/code>\u6a21\u5757\u672c\u8eab\u5c31\u662f\u4e3a\u4e86\u89e3\u51b3\u8de8\u5e73\u53f0\u8def\u5f84\u95ee\u9898\u800c\u8bbe\u8ba1\u7684\u3002\u4f7f\u7528<code>Path<\/code>\u5bf9\u8c61\u8fdb\u884c\u8def\u5f84\u64cd\u4f5c\u65f6\uff0c\u65e0\u9700\u62c5\u5fc3\u8def\u5f84\u5206\u9694\u7b26\u7684\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">path = Path(&#39;folder&#39;) \/ &#39;subfolder&#39; \/ &#39;file.txt&#39;<\/p>\n<p>print(&quot;Joined Path:&quot;, path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7<code>Path<\/code>\u5bf9\u8c61\u8fdb\u884c\u8def\u5f84\u64cd\u4f5c\uff0c\u53ef\u4ee5\u5927\u5927\u63d0\u9ad8\u4ee3\u7801\u7684\u8de8\u5e73\u53f0\u517c\u5bb9\u6027\u3002<\/p>\n<\/p>\n<p><p>\u4e03\u3001\u603b\u7ed3\u4e0e\u5efa\u8bae<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u67e5\u770b\u5f53\u524d\u76ee\u5f55\u4ee5\u53ca\u8fdb\u884c\u5404\u79cd\u8def\u5f84\u64cd\u4f5c\u6709\u591a\u79cd\u65b9\u6cd5\u53ef\u4f9b\u9009\u62e9\u3002\u6839\u636e\u5177\u4f53\u7684\u9700\u6c42\u548c\u573a\u666f\uff0c\u9009\u62e9\u9002\u5408\u7684\u65b9\u6cd5\u548c\u6a21\u5757\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<p><p>7.1\u3001\u4f7f\u7528OS\u6a21\u5757<\/p>\n<\/p>\n<p><p>\u5bf9\u4e8e\u7b80\u5355\u7684\u8def\u5f84\u83b7\u53d6\u548c\u4fee\u6539\uff0c<code>os<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u76f4\u63a5\u7684\u89e3\u51b3\u65b9\u6848\u3002\u5c24\u5176\u662f\u5728\u9700\u8981\u652f\u6301Python 2\u7248\u672c\u65f6\uff0c<code>os<\/code>\u6a21\u5757\u7684\u517c\u5bb9\u6027\u66f4\u597d\u3002<\/p>\n<\/p>\n<p><p>7.2\u3001\u4f7f\u7528PATHLIB\u6a21\u5757<\/p>\n<\/p>\n<p><p>\u5bf9\u4e8e\u66f4\u590d\u6742\u7684\u8def\u5f84\u64cd\u4f5c\u548c\u8de8\u5e73\u53f0\u652f\u6301\uff0c<code>pathlib<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u66f4\u4e3a\u4f18\u96c5\u7684\u89e3\u51b3\u65b9\u6848\u3002\u5176\u9762\u5411\u5bf9\u8c61\u7684\u8bbe\u8ba1\u4f7f\u5f97\u4ee3\u7801\u66f4\u52a0\u6e05\u6670\u6613\u61c2\u3002<\/p>\n<\/p>\n<p><p>7.3\u3001\u7f16\u5199\u5065\u58ee\u7684\u4ee3\u7801<\/p>\n<\/p>\n<p><p>\u65e0\u8bba\u4f7f\u7528\u54ea\u4e2a\u6a21\u5757\uff0c\u90fd\u5e94\u6ce8\u610f\u4ee3\u7801\u7684\u5065\u58ee\u6027\u3002\u901a\u8fc7\u4f7f\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u548c\u9519\u8bef\u5904\u7406\u673a\u5236\uff0c\u53ef\u4ee5\u6709\u6548\u9632\u6b62\u8d44\u6e90\u6cc4\u6f0f\u548c\u7a0b\u5e8f\u5d29\u6e83\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u65b9\u6cd5\u548c\u5efa\u8bae\uff0c\u4f60\u53ef\u4ee5\u66f4\u52a0\u9ad8\u6548\u5730\u8fdb\u884cPython\u7684\u8def\u5f84\u64cd\u4f5c\uff0c\u786e\u4fdd\u7a0b\u5e8f\u5728\u4e0d\u540c\u73af\u5883\u4e0b\u7684\u6b63\u5e38\u8fd0\u884c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728 Python \u4e2d\u83b7\u53d6\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\uff1f<\/strong><br \/>\u5728 Python \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>os<\/code> \u6a21\u5757\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>\u8fd9\u4e2a\u65b9\u6cd5\u975e\u5e38\u7b80\u5355\uff0c\u6267\u884c\u540e\u5373\u53ef\u663e\u793a\u5f53\u524d\u7684\u5de5\u4f5c\u8def\u5f84\u3002<\/p>\n<p><strong>\u5728 Python \u4e2d\u5982\u4f55\u6539\u53d8\u5f53\u524d\u76ee\u5f55\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528 <code>os<\/code> \u6a21\u5757\u7684 <code>chdir()<\/code> \u65b9\u6cd5\u6765\u6539\u53d8\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">import os\nos.chdir(&#39;\/path\/to\/directory&#39;)\nprint(&quot;\u5df2\u66f4\u6539\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55&quot;)\n<\/code><\/pre>\n<p>\u786e\u4fdd\u63d0\u4f9b\u7684\u8def\u5f84\u662f\u6709\u6548\u7684\uff0c\u5426\u5219\u4f1a\u5f15\u53d1\u9519\u8bef\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728 Python \u4e2d\u5217\u51fa\u5f53\u524d\u76ee\u5f55\u4e0b\u7684\u6240\u6709\u6587\u4ef6\u548c\u6587\u4ef6\u5939\uff1f<\/strong><br \/>\u4f7f\u7528 <code>os<\/code> \u6a21\u5757\u7684 <code>listdir()<\/code> \u65b9\u6cd5\u53ef\u4ee5\u5217\u51fa\u5f53\u524d\u76ee\u5f55\u4e0b\u7684\u6240\u6709\u6587\u4ef6\u548c\u6587\u4ef6\u5939\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import os\nfiles_and_dirs = os.listdir(os.getcwd())\nprint(&quot;\u5f53\u524d\u76ee\u5f55\u4e0b\u7684\u6587\u4ef6\u548c\u6587\u4ef6\u5939:&quot;, files_and_dirs)\n<\/code><\/pre>\n<p>\u8fd9\u5c06\u8fd4\u56de\u4e00\u4e2a\u5217\u8868\uff0c\u5305\u542b\u5f53\u524d\u76ee\u5f55\u4e2d\u7684\u6240\u6709\u6761\u76ee\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u67e5\u770b\u5f53\u524d\u76ee\u5f55\u7684Python\u4ee3\u7801\uff0c\u901a\u5e38\u4f7f\u7528os\u6a21\u5757\u548cos.getcwd()\u51fd\u6570\u3001pathlib\u6a21\u5757\u548cPath. [&hellip;]","protected":false},"author":3,"featured_media":953485,"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\/953474"}],"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=953474"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/953474\/revisions"}],"predecessor-version":[{"id":953488,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/953474\/revisions\/953488"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/953485"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=953474"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=953474"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=953474"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}