{"id":1004743,"date":"2024-12-27T10:29:16","date_gmt":"2024-12-27T02:29:16","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1004743.html"},"modified":"2024-12-27T10:29:18","modified_gmt":"2024-12-27T02:29:18","slug":"%e5%a6%82%e4%bd%95%e8%ae%a9python%e6%98%be%e7%a4%ba%e8%b7%af%e5%be%84","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1004743.html","title":{"rendered":"\u5982\u4f55\u8ba9python\u663e\u793a\u8def\u5f84"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25081546\/942e52f6-113f-4bca-9155-631abcaea38d.webp\" alt=\"\u5982\u4f55\u8ba9python\u663e\u793a\u8def\u5f84\" \/><\/p>\n<p><p> <strong>\u8981\u8ba9Python\u663e\u793a\u8def\u5f84\uff0c\u53ef\u4ee5\u4f7f\u7528<code>os<\/code>\u6a21\u5757\u3001<code>sys<\/code>\u6a21\u5757\u3001\u4ee5\u53ca<code>pathlib<\/code>\u6a21\u5757\u3002<\/strong>\u8fd9\u4e9b\u6a21\u5757\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u5904\u7406\u6587\u4ef6\u548c\u76ee\u5f55\u8def\u5f84\u3002\u5176\u4e2d\uff0c<code>os<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u57fa\u672c\u7684\u6587\u4ef6\u548c\u76ee\u5f55\u64cd\u4f5c\u529f\u80fd\uff0c<code>sys<\/code>\u6a21\u5757\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u83b7\u53d6Python\u73af\u5883\u7684\u76f8\u5173\u4fe1\u606f\uff0c\u800c<code>pathlib<\/code>\u6a21\u5757\u5219\u63d0\u4f9b\u4e86\u9762\u5411\u5bf9\u8c61\u7684\u8def\u5f84\u64cd\u4f5c\u65b9\u5f0f\u3002\u4e0b\u9762\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u63a2\u8ba8\u8fd9\u4e9b\u6a21\u5757\u7684\u4f7f\u7528\u65b9\u6cd5\u53ca\u5176\u5e94\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001OS\u6a21\u5757<\/p>\n<\/p>\n<p><p><code>os<\/code>\u6a21\u5757\u662fPython\u4e2d\u5904\u7406\u6587\u4ef6\u548c\u76ee\u5f55\u64cd\u4f5c\u7684\u57fa\u7840\u6a21\u5757\u3002\u5b83\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u51fd\u6570\u7528\u4e8e\u64cd\u4f5c\u548c\u67e5\u8be2\u64cd\u4f5c\u7cfb\u7edf\u7684\u6587\u4ef6\u7cfb\u7edf\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u83b7\u53d6\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55<\/strong><\/li>\n<\/ol>\n<p><p>\u4f7f\u7528<code>os.getcwd()<\/code>\u53ef\u4ee5\u83b7\u53d6\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u7684\u8def\u5f84\u3002\u8fd9\u5728\u9700\u8981\u77e5\u9053\u811a\u672c\u5728\u4f55\u5904\u6267\u884c\u65f6\u7279\u522b\u6709\u7528\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>current_directory = os.getcwd()<\/p>\n<p>print(f&quot;Current Directory: {current_directory}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6b64\u65b9\u6cd5\u8fd4\u56de\u5f53\u524d\u811a\u672c\u6267\u884c\u65f6\u6240\u5728\u7684\u76ee\u5f55\u8def\u5f84\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u5217\u51fa\u76ee\u5f55\u5185\u5bb9<\/strong><\/li>\n<\/ol>\n<p><p><code>os.listdir(path)<\/code>\u53ef\u4ee5\u5217\u51fa\u6307\u5b9a\u8def\u5f84\u4e0b\u7684\u6240\u6709\u6587\u4ef6\u548c\u76ee\u5f55\u3002\u8fd9\u5728\u9700\u8981\u904d\u5386\u67d0\u4e2a\u76ee\u5f55\u7684\u5185\u5bb9\u65f6\u975e\u5e38\u6709\u7528\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">path = &quot;\/some\/directory&quot;<\/p>\n<p>contents = os.listdir(path)<\/p>\n<p>print(f&quot;Contents of {path}: {contents}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u5c06\u8f93\u51fa\u6307\u5b9a\u76ee\u5f55\u4e2d\u6240\u6709\u6587\u4ef6\u548c\u76ee\u5f55\u7684\u540d\u79f0\u3002<\/p>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u8def\u5f84\u62fc\u63a5<\/strong><\/li>\n<\/ol>\n<p><p>\u5f53\u9700\u8981\u6784\u5efa\u590d\u6742\u8def\u5f84\u65f6\uff0c<code>os.path.join()<\/code>\u662f\u4e00\u4e2a\u975e\u5e38\u65b9\u4fbf\u7684\u5de5\u5177\u3002\u5b83\u53ef\u4ee5\u6839\u636e\u64cd\u4f5c\u7cfb\u7edf\u81ea\u52a8\u9009\u62e9\u6b63\u786e\u7684\u8def\u5f84\u5206\u9694\u7b26\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">base_path = &quot;\/some\/directory&quot;<\/p>\n<p>file_name = &quot;example.txt&quot;<\/p>\n<p>full_path = os.path.join(base_path, file_name)<\/p>\n<p>print(f&quot;Full Path: {full_path}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u4e3a\u60a8\u63d0\u4f9b\u4e00\u4e2a\u9002\u7528\u4e8e\u4efb\u4f55\u64cd\u4f5c\u7cfb\u7edf\u7684\u5b8c\u6574\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001SYS\u6a21\u5757<\/p>\n<\/p>\n<p><p><code>sys<\/code>\u6a21\u5757\u5728\u5904\u7406Python\u89e3\u91ca\u5668\u73af\u5883\u548c\u547d\u4ee4\u884c\u53c2\u6570\u65f6\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u83b7\u53d6\u811a\u672c\u8def\u5f84<\/strong><\/li>\n<\/ol>\n<p><p>\u4f7f\u7528<code>sys.argv<\/code>\u53ef\u4ee5\u83b7\u53d6\u6267\u884cPython\u811a\u672c\u65f6\u4f20\u9012\u7684\u547d\u4ee4\u884c\u53c2\u6570\uff0c\u5176\u4e2d<code>sys.argv[0]<\/code>\u901a\u5e38\u662f\u811a\u672c\u7684\u8def\u5f84\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>script_path = sys.argv[0]<\/p>\n<p>print(f&quot;Script Path: {script_path}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5bf9\u4e8e\u9700\u8981\u77e5\u9053\u811a\u672c\u7684\u5b9e\u9645\u4f4d\u7f6e\u7684\u60c5\u51b5\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u83b7\u53d6Python\u8def\u5f84<\/strong><\/li>\n<\/ol>\n<p><p><code>sys.path<\/code>\u662f\u4e00\u4e2a\u5217\u8868\uff0c\u5305\u542b\u4e86Python\u5728\u5bfb\u627e\u6a21\u5757\u65f6\u4f1a\u641c\u7d22\u7684\u8def\u5f84\u3002\u8fd9\u5bf9\u4e8e\u8c03\u8bd5\u6a21\u5757\u5bfc\u5165\u95ee\u9898\u65f6\u975e\u5e38\u6709\u7528\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>print(&quot;Python Path:&quot;)<\/p>\n<p>for p in sys.path:<\/p>\n<p>    print(p)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u663e\u793aPython\u89e3\u91ca\u5668\u5728\u641c\u7d22\u6a21\u5757\u65f6\u4f9d\u6b21\u68c0\u67e5\u7684\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001PATHLIB\u6a21\u5757<\/p>\n<\/p>\n<p><p><code>pathlib<\/code>\u6a21\u5757\u5728Python 3.4\u4e2d\u5f15\u5165\uff0c\u63d0\u4f9b\u4e86\u4e00\u79cd\u9762\u5411\u5bf9\u8c61\u7684\u8def\u5f84\u64cd\u4f5c\u65b9\u6cd5\u3002\u5b83\u4f7f\u8def\u5f84\u64cd\u4f5c\u66f4\u4e3a\u76f4\u89c2\u548c\u7b80\u6d01\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u521b\u5efa\u8def\u5f84\u5bf9\u8c61<\/strong><\/li>\n<\/ol>\n<p><p>\u4f7f\u7528<code>Path<\/code>\u7c7b\u53ef\u4ee5\u521b\u5efa\u8def\u5f84\u5bf9\u8c61\u3002\u8def\u5f84\u5bf9\u8c61\u53ef\u4ee5\u662f\u6587\u4ef6\u6216\u76ee\u5f55\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<p>path = Path(&quot;\/some\/directory&quot;)<\/p>\n<p>print(f&quot;Path: {path}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8def\u5f84\u5bf9\u8c61\u53ef\u4ee5\u901a\u8fc7\u5404\u79cd\u65b9\u6cd5\u8fdb\u884c\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u68c0\u67e5\u8def\u5f84\u662f\u5426\u5b58\u5728<\/strong><\/li>\n<\/ol>\n<p><p>\u8def\u5f84\u5bf9\u8c61\u53ef\u4ee5\u76f4\u63a5\u7528\u4e8e\u68c0\u67e5\u8def\u5f84\u662f\u5426\u5b58\u5728\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<p>path = Path(&quot;\/some\/directory&quot;)<\/p>\n<p>if path.exists():<\/p>\n<p>    print(f&quot;{path} exists.&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(f&quot;{path} does not exist.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5728\u8fdb\u884c\u6587\u4ef6\u6216\u76ee\u5f55\u64cd\u4f5c\u4e4b\u524d\u8fdb\u884c\u5fc5\u8981\u7684\u68c0\u67e5\u3002<\/p>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u904d\u5386\u76ee\u5f55<\/strong><\/li>\n<\/ol>\n<p><p><code>pathlib<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u66f4\u4e3a\u7b80\u6d01\u7684\u76ee\u5f55\u904d\u5386\u65b9\u6cd5\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<p>directory = Path(&quot;\/some\/directory&quot;)<\/p>\n<p>for item in directory.iterdir():<\/p>\n<p>    print(item)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u5217\u51fa\u76ee\u5f55\u4e2d\u7684\u6240\u6709\u6587\u4ef6\u548c\u5b50\u76ee\u5f55\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u65e0\u8bba\u662f<code>os<\/code>\u6a21\u5757\u3001<code>sys<\/code>\u6a21\u5757\u8fd8\u662f<code>pathlib<\/code>\u6a21\u5757\uff0c\u5b83\u4eec\u90fd\u4e3aPython\u7a0b\u5e8f\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u8def\u5f84\u64cd\u4f5c\u529f\u80fd\u3002<strong>\u9009\u62e9\u54ea\u4e2a\u6a21\u5757\u53d6\u51b3\u4e8e\u60a8\u7684\u5177\u4f53\u9700\u6c42\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li><strong>\u5982\u679c\u9700\u8981\u7b80\u5355\u7684\u8def\u5f84\u64cd\u4f5c\uff0c<code>os<\/code>\u6a21\u5757\u53ef\u80fd\u662f\u60a8\u7684\u6700\u4f73\u9009\u62e9\u3002<\/strong><\/li>\n<li><strong>\u5982\u679c\u9700\u8981\u4e0ePython\u73af\u5883\u4ea4\u4e92\uff0c<code>sys<\/code>\u6a21\u5757\u5c06\u975e\u5e38\u6709\u7528\u3002<\/strong><\/li>\n<li><strong>\u5982\u679c\u60a8\u66f4\u559c\u6b22\u9762\u5411\u5bf9\u8c61\u7684\u7f16\u7a0b\u98ce\u683c\uff0c<code>pathlib<\/code>\u6a21\u5757\u662f\u4e00\u4e2a\u975e\u5e38\u597d\u7684\u9009\u62e9\u3002<\/strong><\/li>\n<\/ul>\n<p><p>\u901a\u8fc7\u719f\u7ec3\u638c\u63e1\u8fd9\u4e9b\u6a21\u5757\uff0c\u60a8\u53ef\u4ee5\u5728Python\u4e2d\u8f7b\u677e\u5904\u7406\u5404\u79cd\u8def\u5f84\u64cd\u4f5c\u4efb\u52a1\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\u7684<code>getcwd()<\/code>\u51fd\u6570\u6765\u83b7\u53d6\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u3002\u53ea\u9700\u5bfc\u5165<code>os<\/code>\u6a21\u5757\u5e76\u8c03\u7528<code>os.getcwd()<\/code>\uff0c\u5373\u53ef\u8fd4\u56de\u5f53\u524d\u6267\u884c\u811a\u672c\u7684\u5b8c\u6574\u8def\u5f84\u3002\u4f8b\u5982\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><strong>\u5982\u4f55\u5728Python\u4e2d\u6539\u53d8\u5de5\u4f5c\u76ee\u5f55\uff1f<\/strong><br \/>\u4f7f\u7528<code>os<\/code>\u6a21\u5757\u7684<code>chdir()<\/code>\u51fd\u6570\u53ef\u4ee5\u6539\u53d8\u5f53\u524d\u7684\u5de5\u4f5c\u76ee\u5f55\u3002\u53ea\u9700\u63d0\u4f9b\u76ee\u6807\u8def\u5f84\u4f5c\u4e3a\u53c2\u6570\uff0c\u8c03\u7528<code>os.chdir()<\/code>\u5373\u53ef\u3002\u786e\u4fdd\u63d0\u4f9b\u7684\u8def\u5f84\u662f\u6709\u6548\u7684\uff0c\u5426\u5219\u4f1a\u5f15\u53d1\u9519\u8bef\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">import os\nos.chdir(&#39;\/path\/to\/directory&#39;)  # \u66ff\u6362\u4e3a\u4f60\u60f3\u8981\u7684\u8def\u5f84\nprint(&quot;\u5de5\u4f5c\u76ee\u5f55\u5df2\u66f4\u6539\u4e3a:&quot;, os.getcwd())\n<\/code><\/pre>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u663e\u793a\u7279\u5b9a\u6587\u4ef6\u7684\u7edd\u5bf9\u8def\u5f84\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528<code>os.path<\/code>\u6a21\u5757\u4e2d\u7684<code>abspath()<\/code>\u51fd\u6570\u6765\u83b7\u53d6\u7279\u5b9a\u6587\u4ef6\u7684\u7edd\u5bf9\u8def\u5f84\u3002\u4f20\u5165\u6587\u4ef6\u540d\u4f5c\u4e3a\u53c2\u6570\uff0c\u8fd4\u56de\u7684\u7ed3\u679c\u5c31\u662f\u8be5\u6587\u4ef6\u7684\u5b8c\u6574\u8def\u5f84\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">import os\nfile_path = &#39;example.txt&#39;  # \u66ff\u6362\u4e3a\u4f60\u7684\u6587\u4ef6\u540d\nabsolute_path = os.path.abspath(file_path)\nprint(&quot;\u6587\u4ef6\u7684\u7edd\u5bf9\u8def\u5f84\u662f:&quot;, absolute_path)\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"\u8981\u8ba9Python\u663e\u793a\u8def\u5f84\uff0c\u53ef\u4ee5\u4f7f\u7528os\u6a21\u5757\u3001sys\u6a21\u5757\u3001\u4ee5\u53capathlib\u6a21\u5757\u3002\u8fd9\u4e9b\u6a21\u5757\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u5904\u7406\u6587 [&hellip;]","protected":false},"author":3,"featured_media":1004749,"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\/1004743"}],"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=1004743"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1004743\/revisions"}],"predecessor-version":[{"id":1004752,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1004743\/revisions\/1004752"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1004749"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1004743"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1004743"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1004743"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}