{"id":977077,"date":"2024-12-27T06:26:37","date_gmt":"2024-12-26T22:26:37","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/977077.html"},"modified":"2024-12-27T06:26:39","modified_gmt":"2024-12-26T22:26:39","slug":"python%e5%a6%82%e4%bd%95%e6%9f%a5%e7%9c%8b%e6%96%87%e4%bb%b6%e4%bf%a1%e6%81%af","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/977077.html","title":{"rendered":"python\u5982\u4f55\u67e5\u770b\u6587\u4ef6\u4fe1\u606f"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24202510\/0e3505ac-8c12-4826-88aa-d55e00c9043b.webp\" alt=\"python\u5982\u4f55\u67e5\u770b\u6587\u4ef6\u4fe1\u606f\" \/><\/p>\n<p><p> <strong>\u4f7f\u7528Python\u67e5\u770b\u6587\u4ef6\u4fe1\u606f\u7684\u5e38\u7528\u65b9\u6cd5\u5305\u62ecos\u6a21\u5757\u3001os.path\u6a21\u5757\u548cstat\u6a21\u5757\uff0c\u8fd9\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u83b7\u53d6\u6587\u4ef6\u7684\u5927\u5c0f\u3001\u4fee\u6539\u65f6\u95f4\u3001\u521b\u5efa\u65f6\u95f4\u7b49\u4fe1\u606f\u3002os\u6a21\u5757\u63d0\u4f9b\u4e86\u4e0e\u64cd\u4f5c\u7cfb\u7edf\u4ea4\u4e92\u7684\u529f\u80fd\uff0cos.path\u6a21\u5757\u4e3b\u8981\u7528\u4e8e\u5904\u7406\u6587\u4ef6\u8def\u5f84\uff0c\u800cstat\u6a21\u5757\u5219\u7528\u4e8e\u83b7\u53d6\u6587\u4ef6\u7684\u8be6\u7ec6\u72b6\u6001\u4fe1\u606f\u3002<\/strong>\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u6765\u67e5\u770b\u6587\u4ef6\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001OS\u6a21\u5757<\/p>\n<\/p>\n<p><p>OS\u6a21\u5757\u662fPython\u5185\u7f6e\u7684\u4e00\u4e2a\u6807\u51c6\u5e93\uff0c\u63d0\u4f9b\u4e86\u4e0e\u64cd\u4f5c\u7cfb\u7edf\u8fdb\u884c\u4ea4\u4e92\u7684\u529f\u80fd\u3002\u4f7f\u7528OS\u6a21\u5757\u53ef\u4ee5\u83b7\u53d6\u6587\u4ef6\u7684\u4e00\u4e9b\u57fa\u672c\u4fe1\u606f\uff0c\u6bd4\u5982\u6587\u4ef6\u7684\u5927\u5c0f\u3001\u6587\u4ef6\u662f\u5426\u5b58\u5728\u7b49\u3002<\/p>\n<\/p>\n<ol>\n<li>\u83b7\u53d6\u6587\u4ef6\u5927\u5c0f<\/li>\n<\/ol>\n<p><p>\u8981\u83b7\u53d6\u6587\u4ef6\u7684\u5927\u5c0f\uff0c\u53ef\u4ee5\u4f7f\u7528os.path.getsize()\u51fd\u6570\u3002\u8fd9\u4e2a\u51fd\u6570\u8fd4\u56de\u6587\u4ef6\u7684\u5927\u5c0f\uff08\u4ee5\u5b57\u8282\u4e3a\u5355\u4f4d\uff09\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>file_path = &#39;example.txt&#39;<\/p>\n<p>file_size = os.path.getsize(file_path)<\/p>\n<p>print(f&quot;The size of the file is: {file_size} bytes&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u68c0\u67e5\u6587\u4ef6\u662f\u5426\u5b58\u5728<\/li>\n<\/ol>\n<p><p>\u4f7f\u7528os.path.exists()\u53ef\u4ee5\u68c0\u67e5\u6587\u4ef6\u662f\u5426\u5b58\u5728\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>file_path = &#39;example.txt&#39;<\/p>\n<p>if os.path.exists(file_path):<\/p>\n<p>    print(&quot;The file exists.&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;The file does not exist.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001OS.PATH\u6a21\u5757<\/p>\n<\/p>\n<p><p>os.path\u6a21\u5757\u4e3b\u8981\u7528\u4e8e\u5904\u7406\u6587\u4ef6\u8def\u5f84\u3002\u53ef\u4ee5\u4f7f\u7528\u8fd9\u4e2a\u6a21\u5757\u6765\u83b7\u53d6\u6587\u4ef6\u7684\u540d\u79f0\u3001\u76ee\u5f55\u3001\u6269\u5c55\u540d\u7b49\u4fe1\u606f\u3002<\/p>\n<\/p>\n<ol>\n<li>\u83b7\u53d6\u6587\u4ef6\u7684\u540d\u79f0\u548c\u8def\u5f84<\/li>\n<\/ol>\n<p><p>\u53ef\u4ee5\u4f7f\u7528os.path.basename()\u548cos.path.dirname()\u6765\u83b7\u53d6\u6587\u4ef6\u7684\u540d\u79f0\u548c\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>file_path = &#39;\/path\/to\/example.txt&#39;<\/p>\n<p>file_name = os.path.basename(file_path)<\/p>\n<p>file_directory = os.path.dirname(file_path)<\/p>\n<p>print(f&quot;File name: {file_name}&quot;)<\/p>\n<p>print(f&quot;File directory: {file_directory}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u83b7\u53d6\u6587\u4ef6\u7684\u6269\u5c55\u540d<\/li>\n<\/ol>\n<p><p>os.path.splitext()\u53ef\u4ee5\u7528\u4e8e\u5206\u79bb\u6587\u4ef6\u540d\u548c\u6269\u5c55\u540d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>file_path = &#39;example.txt&#39;<\/p>\n<p>file_name, file_extension = os.path.splitext(file_path)<\/p>\n<p>print(f&quot;File extension: {file_extension}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001STAT\u6a21\u5757<\/p>\n<\/p>\n<p><p>stat\u6a21\u5757\u63d0\u4f9b\u4e86\u6587\u4ef6\u7684\u8be6\u7ec6\u72b6\u6001\u4fe1\u606f\u3002\u4f7f\u7528os.stat()\u53ef\u4ee5\u83b7\u53d6\u6587\u4ef6\u7684\u8be6\u7ec6\u4fe1\u606f\uff0c\u6bd4\u5982\u6587\u4ef6\u7684\u6743\u9650\u3001\u521b\u5efa\u65f6\u95f4\u3001\u4fee\u6539\u65f6\u95f4\u7b49\u3002<\/p>\n<\/p>\n<ol>\n<li>\u83b7\u53d6\u6587\u4ef6\u7684\u8be6\u7ec6\u4fe1\u606f<\/li>\n<\/ol>\n<p><p>os.stat()\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u6587\u4ef6\u8be6\u7ec6\u4fe1\u606f\u7684\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>file_path = &#39;example.txt&#39;<\/p>\n<p>file_stat = os.stat(file_path)<\/p>\n<p>print(file_stat)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u83b7\u53d6\u6587\u4ef6\u7684\u521b\u5efa\u65f6\u95f4\u548c\u4fee\u6539\u65f6\u95f4<\/li>\n<\/ol>\n<p><p>\u53ef\u4ee5\u4f7f\u7528os.stat()\u7ed3\u5408time\u6a21\u5757\u6765\u83b7\u53d6\u6587\u4ef6\u7684\u521b\u5efa\u65f6\u95f4\u548c\u4fee\u6539\u65f6\u95f4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>import time<\/p>\n<p>file_path = &#39;example.txt&#39;<\/p>\n<p>file_stat = os.stat(file_path)<\/p>\n<p>creation_time = time.ctime(file_stat.st_ctime)<\/p>\n<p>modification_time = time.ctime(file_stat.st_mtime)<\/p>\n<p>print(f&quot;Creation time: {creation_time}&quot;)<\/p>\n<p>print(f&quot;Modification time: {modification_time}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u5176\u4ed6\u65b9\u6cd5<\/p>\n<\/p>\n<p><p>\u9664\u4e86\u4e0a\u8ff0\u65b9\u6cd5\u5916\uff0cPython\u4e2d\u8fd8\u6709\u5176\u4ed6\u4e00\u4e9b\u6a21\u5757\u548c\u5e93\u53ef\u4ee5\u7528\u4e8e\u83b7\u53d6\u6587\u4ef6\u4fe1\u606f\uff0c\u6bd4\u5982pathlib\u6a21\u5757\u3002<\/p>\n<\/p>\n<ol>\n<li>\u4f7f\u7528pathlib\u6a21\u5757<\/li>\n<\/ol>\n<p><p>pathlib\u6a21\u5757\u662fPython 3.4\u5f15\u5165\u7684\u4e00\u4e2a\u6a21\u5757\uff0c\u63d0\u4f9b\u4e86\u9762\u5411\u5bf9\u8c61\u7684\u6587\u4ef6\u7cfb\u7edf\u8def\u5f84\u64cd\u4f5c\u65b9\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<p>file_path = Path(&#39;example.txt&#39;)<\/p>\n<p>file_size = file_path.stat().st_size<\/p>\n<p>modification_time = file_path.stat().st_mtime<\/p>\n<p>print(f&quot;File size: {file_size} bytes&quot;)<\/p>\n<p>print(f&quot;Modification time: {time.ctime(modification_time)}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93<\/li>\n<\/ol>\n<p><p>\u5982\u679c\u9700\u8981\u5904\u7406\u66f4\u591a\u590d\u6742\u7684\u6587\u4ef6\u4fe1\u606f\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\uff0c\u6bd4\u5982PyFilesystem\u3002<\/p>\n<\/p>\n<p><p>\u603b\u7ed3<\/p>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u7528\u4e8e\u67e5\u770b\u6587\u4ef6\u4fe1\u606f\uff0c\u5305\u62ecos\u6a21\u5757\u3001os.path\u6a21\u5757\u3001stat\u6a21\u5757\u548cpathlib\u6a21\u5757\u7b49\u3002\u8fd9\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u8005\u8f7b\u677e\u83b7\u53d6\u6587\u4ef6\u7684\u5927\u5c0f\u3001\u5b58\u5728\u72b6\u6001\u3001\u8def\u5f84\u3001\u540d\u79f0\u3001\u6269\u5c55\u540d\u3001\u521b\u5efa\u65f6\u95f4\u3001\u4fee\u6539\u65f6\u95f4\u7b49\u4fe1\u606f\u3002\u6839\u636e\u9879\u76ee\u7684\u9700\u6c42\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u6765\u5904\u7406\u6587\u4ef6\u4fe1\u606f\u662f\u5f88\u91cd\u8981\u7684\u3002\u901a\u8fc7\u5408\u7406\u5730\u4f7f\u7528\u8fd9\u4e9b\u5de5\u5177\uff0c\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u83b7\u53d6\u6587\u4ef6\u7684\u5927\u5c0f\u548c\u4fee\u6539\u65f6\u95f4\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>os<\/code>\u6a21\u5757\u4e2d\u7684<code>stat<\/code>\u51fd\u6570\u6765\u83b7\u53d6\u6587\u4ef6\u7684\u8be6\u7ec6\u4fe1\u606f\u3002\u901a\u8fc7<code>os.path.getsize()<\/code>\u53ef\u4ee5\u83b7\u53d6\u6587\u4ef6\u5927\u5c0f\uff0c\u800c<code>os.path.getmtime()<\/code>\u5219\u53ef\u4ee5\u83b7\u53d6\u6587\u4ef6\u7684\u6700\u540e\u4fee\u6539\u65f6\u95f4\u3002\u5177\u4f53\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">import os\nfile_path = &#39;\u4f60\u7684\u6587\u4ef6\u8def\u5f84&#39;\nfile_size = os.path.getsize(file_path)\nfile_mod_time = os.path.getmtime(file_path)\nprint(f&quot;\u6587\u4ef6\u5927\u5c0f: {file_size} \u5b57\u8282&quot;)\nprint(f&quot;\u6700\u540e\u4fee\u6539\u65f6\u95f4: {file_mod_time}&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u6837\u5c31\u80fd\u591f\u8f7b\u677e\u67e5\u770b\u6587\u4ef6\u7684\u57fa\u672c\u4fe1\u606f\u3002<\/p>\n<p><strong>\u5982\u4f55\u68c0\u67e5\u6587\u4ef6\u662f\u5426\u5b58\u5728\u4ee5\u53ca\u83b7\u53d6\u5176\u8be6\u7ec6\u4fe1\u606f\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528<code>os.path.exists()<\/code>\u6765\u68c0\u67e5\u6587\u4ef6\u662f\u5426\u5b58\u5728\u3002\u5982\u679c\u5b58\u5728\uff0c\u53ef\u4ee5\u7ee7\u7eed\u4f7f\u7528<code>os.stat()<\/code>\u83b7\u53d6\u66f4\u8be6\u7ec6\u7684\u4fe1\u606f\uff0c\u5305\u62ec\u6587\u4ef6\u7684\u6743\u9650\u3001\u7528\u6237ID\u7b49\u3002\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">import os\nfile_path = &#39;\u4f60\u7684\u6587\u4ef6\u8def\u5f84&#39;\nif os.path.exists(file_path):\n    file_info = os.stat(file_path)\n    print(f&quot;\u6587\u4ef6\u6743\u9650: {file_info.st_mode}&quot;)\n    print(f&quot;\u7528\u6237ID: {file_info.st_uid}&quot;)\nelse:\n    print(&quot;\u6587\u4ef6\u4e0d\u5b58\u5728&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u6837\u80fd\u591f\u786e\u4fdd\u5728\u8fdb\u884c\u6587\u4ef6\u64cd\u4f5c\u4e4b\u524d\uff0c\u6587\u4ef6\u786e\u5b9e\u5b58\u5728\u3002<\/p>\n<p><strong>\u5982\u4f55\u5229\u7528Python\u67e5\u770b\u6587\u4ef6\u7684\u5185\u5bb9\u53ca\u683c\u5f0f\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528<code>open()<\/code>\u51fd\u6570\u6765\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\uff0c\u540c\u65f6\u6839\u636e\u6587\u4ef6\u540e\u7f00\u540d\u53ef\u4ee5\u5224\u65ad\u6587\u4ef6\u683c\u5f0f\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">file_path = &#39;\u4f60\u7684\u6587\u4ef6\u8def\u5f84&#39;\nwith open(file_path, &#39;r&#39;) as file:\n    content = file.read()\n    print(&quot;\u6587\u4ef6\u5185\u5bb9:\\n&quot;, content)\n<\/code><\/pre>\n<p>\u7ed3\u5408<code>os.path.splitext()<\/code>\u53ef\u4ee5\u83b7\u53d6\u6587\u4ef6\u7684\u683c\u5f0f\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u5728\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u7684\u540c\u65f6\u4e86\u89e3\u6587\u4ef6\u7684\u7c7b\u578b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4f7f\u7528Python\u67e5\u770b\u6587\u4ef6\u4fe1\u606f\u7684\u5e38\u7528\u65b9\u6cd5\u5305\u62ecos\u6a21\u5757\u3001os.path\u6a21\u5757\u548cstat\u6a21\u5757\uff0c\u8fd9\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u83b7\u53d6\u6587\u4ef6\u7684\u5927 [&hellip;]","protected":false},"author":3,"featured_media":977084,"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\/977077"}],"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=977077"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/977077\/revisions"}],"predecessor-version":[{"id":977087,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/977077\/revisions\/977087"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/977084"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=977077"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=977077"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=977077"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}