{"id":1097406,"date":"2025-01-08T15:11:32","date_gmt":"2025-01-08T07:11:32","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1097406.html"},"modified":"2025-01-08T15:11:34","modified_gmt":"2025-01-08T07:11:34","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e8%8e%b7%e5%8f%96%e7%9b%ae%e6%a0%87%e6%96%87%e4%bb%b6%e5%88%9b%e5%bb%ba%e6%97%b6%e9%97%b4-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1097406.html","title":{"rendered":"\u5982\u4f55\u7528python\u83b7\u53d6\u76ee\u6807\u6587\u4ef6\u521b\u5efa\u65f6\u95f4"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24212205\/be715c39-ce45-41fe-99df-72b9fd562a66.webp\" alt=\"\u5982\u4f55\u7528python\u83b7\u53d6\u76ee\u6807\u6587\u4ef6\u521b\u5efa\u65f6\u95f4\" \/><\/p>\n<p><p> <strong>\u8981\u7528Python\u83b7\u53d6\u76ee\u6807\u6587\u4ef6\u7684\u521b\u5efa\u65f6\u95f4\uff0c\u53ef\u4ee5\u4f7f\u7528os\u3001pathlib\u6a21\u5757\u3001\u8be6\u7ec6\u7684\u65f6\u95f4\u683c\u5f0f\u5316\u3002<\/strong><\/p>\n<\/p>\n<p><p>Python \u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u83b7\u53d6\u6587\u4ef6\u7684\u521b\u5efa\u65f6\u95f4\uff0c\u5176\u4e2d\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528 <code>os<\/code> \u6a21\u5757\u548c <code>pathlib<\/code> \u6a21\u5757\u3002\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u68c0\u7d22\u548c\u5904\u7406\u6587\u4ef6\u7684\u5143\u6570\u636e\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u6765\u83b7\u53d6\u6587\u4ef6\u7684\u521b\u5efa\u65f6\u95f4\uff0c\u5e76\u63d0\u4f9b\u4e00\u4e9b\u793a\u4f8b\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001OS\u6a21\u5757<\/p>\n<\/p>\n<p><p>Python \u7684 <code>os<\/code> \u6a21\u5757\u63d0\u4f9b\u4e86\u4e0e\u64cd\u4f5c\u7cfb\u7edf\u8fdb\u884c\u4ea4\u4e92\u7684\u529f\u80fd\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 <code>os.path.getctime<\/code> \u51fd\u6570\u6765\u83b7\u53d6\u6587\u4ef6\u7684\u521b\u5efa\u65f6\u95f4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>import time<\/p>\n<h2><strong>\u83b7\u53d6\u6587\u4ef6\u7684\u521b\u5efa\u65f6\u95f4<\/strong><\/h2>\n<p>def get_creation_time(file_path):<\/p>\n<p>    creation_time = os.path.getctime(file_path)<\/p>\n<p>    return creation_time<\/p>\n<h2><strong>\u683c\u5f0f\u5316\u65f6\u95f4\u6233<\/strong><\/h2>\n<p>def format_time(timestamp):<\/p>\n<p>    formatted_time = time.strftime(&#39;%Y-%m-%d %H:%M:%S&#39;, time.localtime(timestamp))<\/p>\n<p>    return formatted_time<\/p>\n<p>file_path = &#39;example.txt&#39;  # \u66ff\u6362\u4e3a\u4f60\u7684\u6587\u4ef6\u8def\u5f84<\/p>\n<p>creation_time = get_creation_time(file_path)<\/p>\n<p>formatted_creation_time = format_time(creation_time)<\/p>\n<p>print(f&quot;\u6587\u4ef6\u7684\u521b\u5efa\u65f6\u95f4\u662f: {formatted_creation_time}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u89e3\u91ca\uff1a<\/strong>  <\/p>\n<\/p>\n<ul>\n<li><code>os.path.getctime(file_path)<\/code> \u8fd4\u56de\u7684\u662f\u6587\u4ef6\u7684\u521b\u5efa\u65f6\u95f4\uff0c\u5355\u4f4d\u662f\u79d2\u3002<\/li>\n<li><code>time.strftime<\/code> \u548c <code>time.localtime<\/code> \u7528\u4e8e\u5c06\u65f6\u95f4\u6233\u683c\u5f0f\u5316\u4e3a\u4eba\u7c7b\u53ef\u8bfb\u7684\u65f6\u95f4\u3002<\/li>\n<\/ul>\n<p><p>\u4e8c\u3001Pathlib\u6a21\u5757<\/p>\n<\/p>\n<p><p><code>pathlib<\/code> \u662f Python 3.4 \u5f15\u5165\u7684\u6a21\u5757\uff0c\u7528\u4e8e\u9762\u5411\u5bf9\u8c61\u5730\u64cd\u4f5c\u6587\u4ef6\u7cfb\u7edf\u8def\u5f84\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 <code>Path<\/code> \u5bf9\u8c61\u7684 <code>stat<\/code> \u65b9\u6cd5\u6765\u83b7\u53d6\u6587\u4ef6\u7684\u521b\u5efa\u65f6\u95f4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<p>import time<\/p>\n<h2><strong>\u83b7\u53d6\u6587\u4ef6\u7684\u521b\u5efa\u65f6\u95f4<\/strong><\/h2>\n<p>def get_creation_time(file_path):<\/p>\n<p>    path = Path(file_path)<\/p>\n<p>    creation_time = path.stat().st_ctime<\/p>\n<p>    return creation_time<\/p>\n<h2><strong>\u683c\u5f0f\u5316\u65f6\u95f4\u6233<\/strong><\/h2>\n<p>def format_time(timestamp):<\/p>\n<p>    formatted_time = time.strftime(&#39;%Y-%m-%d %H:%M:%S&#39;, time.localtime(timestamp))<\/p>\n<p>    return formatted_time<\/p>\n<p>file_path = &#39;example.txt&#39;  # \u66ff\u6362\u4e3a\u4f60\u7684\u6587\u4ef6\u8def\u5f84<\/p>\n<p>creation_time = get_creation_time(file_path)<\/p>\n<p>formatted_creation_time = format_time(creation_time)<\/p>\n<p>print(f&quot;\u6587\u4ef6\u7684\u521b\u5efa\u65f6\u95f4\u662f: {formatted_creation_time}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u89e3\u91ca\uff1a<\/strong>  <\/p>\n<\/p>\n<ul>\n<li><code>Path(file_path).stat().st_ctime<\/code> \u8fd4\u56de\u7684\u662f\u6587\u4ef6\u7684\u521b\u5efa\u65f6\u95f4\uff0c\u5355\u4f4d\u662f\u79d2\u3002<\/li>\n<li><code>time.strftime<\/code> \u548c <code>time.localtime<\/code> \u7528\u4e8e\u5c06\u65f6\u95f4\u6233\u683c\u5f0f\u5316\u4e3a\u4eba\u7c7b\u53ef\u8bfb\u7684\u65f6\u95f4\u3002<\/li>\n<\/ul>\n<p><p>\u4e09\u3001\u8be6\u7ec6\u63cf\u8ff0\u521b\u5efa\u65f6\u95f4\u7684\u683c\u5f0f\u5316<\/p>\n<\/p>\n<p><p>\u5f53\u6211\u4eec\u83b7\u53d6\u6587\u4ef6\u7684\u521b\u5efa\u65f6\u95f4\u65f6\uff0c\u901a\u5e38\u4f1a\u5f97\u5230\u4e00\u4e2a\u65f6\u95f4\u6233\u3002\u65f6\u95f4\u6233\u8868\u793a\u4ece1970\u5e741\u67081\u65e500:00:00 UTC\u5230\u73b0\u5728\u7684\u79d2\u6570\u3002\u4e3a\u4e86\u8ba9\u65f6\u95f4\u4fe1\u606f\u66f4\u52a0\u76f4\u89c2\u548c\u6613\u4e8e\u7406\u89e3\uff0c\u6211\u4eec\u9700\u8981\u5bf9\u65f6\u95f4\u6233\u8fdb\u884c\u683c\u5f0f\u5316\u3002<\/p>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86 <code>time.strftime<\/code> \u548c <code>time.localtime<\/code> \u6765\u683c\u5f0f\u5316\u65f6\u95f4\u6233\u3002<code>time.localtime<\/code> \u5c06\u65f6\u95f4\u6233\u8f6c\u6362\u4e3a\u672c\u5730\u65f6\u95f4\u7684 <code>struct_time<\/code> \u5bf9\u8c61\uff0c\u800c <code>time.strftime<\/code> \u5219\u5c06 <code>struct_time<\/code> \u5bf9\u8c61\u683c\u5f0f\u5316\u4e3a\u6307\u5b9a\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<p>timestamp = 1627885447  # \u793a\u4f8b\u65f6\u95f4\u6233<\/p>\n<p>struct_time = time.localtime(timestamp)<\/p>\n<p>formatted_time = time.strftime(&#39;%Y-%m-%d %H:%M:%S&#39;, struct_time)<\/p>\n<p>print(f&quot;\u683c\u5f0f\u5316\u540e\u7684\u65f6\u95f4\u662f: {formatted_time}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u89e3\u91ca\uff1a<\/strong>  <\/p>\n<\/p>\n<ul>\n<li><code>time.localtime(timestamp)<\/code> \u5c06\u65f6\u95f4\u6233\u8f6c\u6362\u4e3a <code>struct_time<\/code> \u5bf9\u8c61\uff0c\u5176\u4e2d\u5305\u542b\u5e74\u3001\u6708\u3001\u65e5\u3001\u65f6\u3001\u5206\u3001\u79d2\u7b49\u4fe1\u606f\u3002<\/li>\n<li><code>time.strftime(&#39;%Y-%m-%d %H:%M:%S&#39;, struct_time)<\/code> \u5c06 <code>struct_time<\/code> \u5bf9\u8c61\u683c\u5f0f\u5316\u4e3a\u5b57\u7b26\u4e32\uff0c\u4f8b\u5982 <code>2021-08-02 15:04:07<\/code>\u3002<\/li>\n<\/ul>\n<p><p>\u56db\u3001\u8de8\u5e73\u53f0\u6ce8\u610f\u4e8b\u9879<\/p>\n<\/p>\n<p><p>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c<code>os.path.getctime<\/code> \u548c <code>Path.stat().st_ctime<\/code> \u5728\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u4e0a\u7684\u884c\u4e3a\u53ef\u80fd\u6709\u6240\u4e0d\u540c\u3002\u5728\u67d0\u4e9b\u64cd\u4f5c\u7cfb\u7edf\uff08\u4f8b\u5982Windows\uff09\u4e0a\uff0c\u5b83\u4eec\u8fd4\u56de\u7684\u662f\u6587\u4ef6\u7684\u521b\u5efa\u65f6\u95f4\uff0c\u800c\u5728\u5176\u4ed6\u64cd\u4f5c\u7cfb\u7edf\uff08\u4f8b\u5982Unix\uff09\u4e0a\uff0c\u5b83\u4eec\u8fd4\u56de\u7684\u662f\u6587\u4ef6\u7684\u6700\u540e\u72b6\u6001\u66f4\u6539\u65f6\u95f4\u3002<\/p>\n<\/p>\n<p><p>\u4e3a\u4e86\u786e\u4fdd\u4ee3\u7801\u5728\u6240\u6709\u5e73\u53f0\u4e0a\u90fd\u80fd\u6b63\u786e\u8fd0\u884c\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>platform<\/code> \u6a21\u5757\u6765\u68c0\u6d4b\u64cd\u4f5c\u7cfb\u7edf\uff0c\u5e76\u76f8\u5e94\u5730\u9009\u62e9\u9002\u5f53\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>import platform<\/p>\n<p>import time<\/p>\n<p>from pathlib import Path<\/p>\n<p>def get_creation_time(file_path):<\/p>\n<p>    if platform.system() == &#39;Windows&#39;:<\/p>\n<p>        creation_time = os.path.getctime(file_path)<\/p>\n<p>    else:<\/p>\n<p>        stat = Path(file_path).stat()<\/p>\n<p>        try:<\/p>\n<p>            creation_time = stat.st_birthtime<\/p>\n<p>        except AttributeError:<\/p>\n<p>            creation_time = stat.st_ctime<\/p>\n<p>    return creation_time<\/p>\n<p>def format_time(timestamp):<\/p>\n<p>    formatted_time = time.strftime(&#39;%Y-%m-%d %H:%M:%S&#39;, time.localtime(timestamp))<\/p>\n<p>    return formatted_time<\/p>\n<p>file_path = &#39;example.txt&#39;  # \u66ff\u6362\u4e3a\u4f60\u7684\u6587\u4ef6\u8def\u5f84<\/p>\n<p>creation_time = get_creation_time(file_path)<\/p>\n<p>formatted_creation_time = format_time(creation_time)<\/p>\n<p>print(f&quot;\u6587\u4ef6\u7684\u521b\u5efa\u65f6\u95f4\u662f: {formatted_creation_time}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u89e3\u91ca\uff1a<\/strong>  <\/p>\n<\/p>\n<ul>\n<li><code>platform.system()<\/code> \u7528\u4e8e\u68c0\u6d4b\u64cd\u4f5c\u7cfb\u7edf\u3002<\/li>\n<li>Windows\u4e0a\u4f7f\u7528 <code>os.path.getctime<\/code> \u83b7\u53d6\u6587\u4ef6\u7684\u521b\u5efa\u65f6\u95f4\u3002<\/li>\n<li>Unix\u7cfb\u7edf\u4e0a\u5c1d\u8bd5\u4f7f\u7528 <code>st_birthtime<\/code> \u83b7\u53d6\u6587\u4ef6\u7684\u521b\u5efa\u65f6\u95f4\uff0c\u5982\u679c\u4e0d\u53ef\u7528\u5219\u4f7f\u7528 <code>st_ctime<\/code> \u83b7\u53d6\u6587\u4ef6\u7684\u72b6\u6001\u66f4\u6539\u65f6\u95f4\u3002<\/li>\n<\/ul>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u5404\u79cd\u64cd\u4f5c\u7cfb\u7edf\u4e0a\u51c6\u786e\u5730\u83b7\u53d6\u6587\u4ef6\u7684\u521b\u5efa\u65f6\u95f4\uff0c\u5e76\u5c06\u5176\u683c\u5f0f\u5316\u4e3a\u53ef\u8bfb\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u3002\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff01<\/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\u521b\u5efa\u65f6\u95f4\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528<code>os<\/code>\u6a21\u5757\u4e2d\u7684<code>stat()<\/code>\u51fd\u6570\u6765\u83b7\u53d6\u6587\u4ef6\u7684\u521b\u5efa\u65f6\u95f4\u3002\u5177\u4f53\u65b9\u6cd5\u662f\u4f7f\u7528<code>os.path.getctime()<\/code>\u51fd\u6570\uff0c\u5b83\u4f1a\u8fd4\u56de\u6587\u4ef6\u7684\u521b\u5efa\u65f6\u95f4\u6233\u3002\u53ef\u4ee5\u5c06\u8be5\u65f6\u95f4\u6233\u8f6c\u6362\u4e3a\u53ef\u8bfb\u7684\u65f6\u95f4\u683c\u5f0f\uff0c\u4f8b\u5982\u4f7f\u7528<code>datetime<\/code>\u6a21\u5757\u4e2d\u7684<code>fromtimestamp()<\/code>\u65b9\u6cd5\u3002<\/p>\n<p><strong>\u83b7\u53d6\u6587\u4ef6\u521b\u5efa\u65f6\u95f4\u65f6\uff0c\u662f\u5426\u9700\u8981\u7279\u5b9a\u7684\u6743\u9650\uff1f<\/strong><br \/>\u83b7\u53d6\u6587\u4ef6\u521b\u5efa\u65f6\u95f4\u901a\u5e38\u9700\u8981\u5bf9\u8be5\u6587\u4ef6\u7684\u8bfb\u53d6\u6743\u9650\u3002\u5982\u679c\u6587\u4ef6\u7684\u6743\u9650\u8bbe\u7f6e\u4e3a\u53ea\u8bfb\u6216\u53d7\u9650\uff0c\u60a8\u53ef\u80fd\u65e0\u6cd5\u6210\u529f\u83b7\u53d6\u5176\u521b\u5efa\u65f6\u95f4\u3002\u5728\u4f7f\u7528Python\u4ee3\u7801\u65f6\uff0c\u786e\u4fdd\u60a8\u6709\u9002\u5f53\u7684\u8bbf\u95ee\u6743\u9650\uff0c\u4ee5\u907f\u514d\u6743\u9650\u9519\u8bef\u3002<\/p>\n<p><strong>\u5728\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u4e0a\uff0c\u83b7\u53d6\u6587\u4ef6\u521b\u5efa\u65f6\u95f4\u7684\u8868\u73b0\u662f\u5426\u4e00\u81f4\uff1f<\/strong><br \/>\u4e0d\u540c\u7684\u64cd\u4f5c\u7cfb\u7edf\u5728\u5904\u7406\u6587\u4ef6\u521b\u5efa\u65f6\u95f4\u65f6\u53ef\u80fd\u4f1a\u6709\u6240\u4e0d\u540c\u3002\u4f8b\u5982\uff0c\u5728\u67d0\u4e9bLinux\u7cfb\u7edf\u4e0a\uff0c\u6587\u4ef6\u7684\u521b\u5efa\u65f6\u95f4\u53ef\u80fd\u5e76\u4e0d\u603b\u662f\u53ef\u7528\uff0c\u56e0\u4e3a\u5b83\u4eec\u901a\u5e38\u53ea\u4fdd\u5b58\u4fee\u6539\u65f6\u95f4\u548c\u8bbf\u95ee\u65f6\u95f4\u3002Windows\u7cfb\u7edf\u5219\u901a\u5e38\u652f\u6301\u83b7\u53d6\u6587\u4ef6\u7684\u521b\u5efa\u65f6\u95f4\u3002\u4e3a\u4e86\u786e\u4fdd\u8de8\u5e73\u53f0\u517c\u5bb9\u6027\uff0c\u5efa\u8bae\u5728\u4ee3\u7801\u4e2d\u6dfb\u52a0\u9002\u5f53\u7684\u9519\u8bef\u5904\u7406\u548c\u68c0\u67e5\u673a\u5236\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u7528Python\u83b7\u53d6\u76ee\u6807\u6587\u4ef6\u7684\u521b\u5efa\u65f6\u95f4\uff0c\u53ef\u4ee5\u4f7f\u7528os\u3001pathlib\u6a21\u5757\u3001\u8be6\u7ec6\u7684\u65f6\u95f4\u683c\u5f0f\u5316\u3002 Python \u63d0 [&hellip;]","protected":false},"author":3,"featured_media":1097413,"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\/1097406"}],"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=1097406"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1097406\/revisions"}],"predecessor-version":[{"id":1097414,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1097406\/revisions\/1097414"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1097413"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1097406"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1097406"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1097406"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}