{"id":1011651,"date":"2024-12-27T11:31:51","date_gmt":"2024-12-27T03:31:51","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1011651.html"},"modified":"2024-12-27T11:31:56","modified_gmt":"2024-12-27T03:31:56","slug":"python%e5%a6%82%e4%bd%95%e8%8e%b7%e5%8f%96%e6%96%87%e4%bb%b6%e5%88%9b%e5%bb%ba","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1011651.html","title":{"rendered":"python\u5982\u4f55\u83b7\u53d6\u6587\u4ef6\u521b\u5efa"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25085719\/b1b8999f-881c-4d75-80ef-c3929e66cfcc.webp\" alt=\"python\u5982\u4f55\u83b7\u53d6\u6587\u4ef6\u521b\u5efa\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u83b7\u53d6\u6587\u4ef6\u521b\u5efa\u65f6\u95f4\uff0c\u4e3b\u8981\u53ef\u4ee5\u901a\u8fc7os\u6a21\u5757\u3001os.path\u6a21\u5757\u3001\u4ee5\u53ca\u7b2c\u4e09\u65b9\u5e93\u5982pathlib\u6765\u5b9e\u73b0\u3002os\u6a21\u5757\u63d0\u4f9b\u4e86\u4e0e\u64cd\u4f5c\u7cfb\u7edf\u8fdb\u884c\u4ea4\u4e92\u7684\u529f\u80fd\u3001os.path\u6a21\u5757\u63d0\u4f9b\u4e86\u5904\u7406\u6587\u4ef6\u8def\u5f84\u7684\u65b9\u6cd5\u3001pathlib\u63d0\u4f9b\u4e86\u9762\u5411\u5bf9\u8c61\u7684\u6587\u4ef6\u7cfb\u7edf\u8def\u5f84\u64cd\u4f5c\u65b9\u6cd5\u3002<\/strong>\u5728\u8fd9\u4e09\u8005\u4e2d\uff0cos.path.getctime()\u662f\u6700\u5e38\u7528\u7684\u83b7\u53d6\u6587\u4ef6\u521b\u5efa\u65f6\u95f4\u7684\u65b9\u6cd5\uff0c\u5c24\u5176\u5728Windows\u64cd\u4f5c\u7cfb\u7edf\u4e2d\u3002\u63a5\u4e0b\u6765\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001OS\u6a21\u5757<\/p>\n<\/p>\n<p><p>Python\u7684os\u6a21\u5757\u63d0\u4f9b\u4e86\u4e0e\u64cd\u4f5c\u7cfb\u7edf\u4ea4\u4e92\u7684\u529f\u80fd\uff0c\u53ef\u4ee5\u7528\u6765\u83b7\u53d6\u6587\u4ef6\u7684\u521b\u5efa\u65f6\u95f4\u3002\u5728Windows\u64cd\u4f5c\u7cfb\u7edf\u4e2d\uff0cos.path.getctime()\u65b9\u6cd5\u8fd4\u56de\u7684\u662f\u6587\u4ef6\u7684\u521b\u5efa\u65f6\u95f4\uff0c\u800c\u5728Unix\/Linux\u7cfb\u7edf\u4e2d\uff0c\u8fd4\u56de\u7684\u662f\u6700\u540e\u4e00\u6b21\u72b6\u6001\u66f4\u6539\u7684\u65f6\u95f4\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528os\u6a21\u5757\u83b7\u53d6\u6587\u4ef6\u521b\u5efa\u65f6\u95f4\u7684\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u5bfc\u5165os\u6a21\u5757\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u4f7f\u7528os.path.getctime()\u83b7\u53d6\u6587\u4ef6\u521b\u5efa\u65f6\u95f4\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">file_path = &#39;example.txt&#39;  # \u9700\u8981\u83b7\u53d6\u521b\u5efa\u65f6\u95f4\u7684\u6587\u4ef6\u8def\u5f84<\/p>\n<p>creation_time = os.path.getctime(file_path)<\/p>\n<p>print(&quot;\u521b\u5efa\u65f6\u95f4\uff1a&quot;, creation_time)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li>\u5c06\u83b7\u53d6\u5230\u7684\u65f6\u95f4\u8f6c\u6362\u4e3a\u53ef\u8bfb\u683c\u5f0f\uff1a<\/li>\n<\/ol>\n<p><p>\u53ef\u4ee5\u4f7f\u7528time\u6a21\u5757\u4e2d\u7684strftime()\u51fd\u6570\u5c06\u65f6\u95f4\u6233\u8f6c\u6362\u4e3a\u53ef\u8bfb\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<p>readable_time = time.strftime(&#39;%Y-%m-%d %H:%M:%S&#39;, time.localtime(creation_time))<\/p>\n<p>print(&quot;\u53ef\u8bfb\u683c\u5f0f\u7684\u521b\u5efa\u65f6\u95f4\uff1a&quot;, readable_time)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u5728Windows\u7cfb\u7edf\u4e0a\u4f7f\u7528\u5e7f\u6cdb\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u76f4\u63a5\u83b7\u53d6\u6587\u4ef6\u7684\u521b\u5efa\u65f6\u95f4\uff0c\u800c\u4e0d\u4f9d\u8d56\u4e8e\u6587\u4ef6\u7684\u5176\u4ed6\u5c5e\u6027\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001Pathlib\u6a21\u5757<\/p>\n<\/p>\n<p><p>\u5728Python 3.4\u53ca\u4ee5\u540e\u7684\u7248\u672c\u4e2d\uff0cpathlib\u6a21\u5757\u63d0\u4f9b\u4e86\u66f4\u52a0\u9762\u5411\u5bf9\u8c61\u7684\u6587\u4ef6\u7cfb\u7edf\u8def\u5f84\u64cd\u4f5c\u65b9\u6cd5\u3002Pathlib\u6a21\u5757\u4e2d\u7684Path\u7c7b\u63d0\u4f9b\u4e86\u83b7\u53d6\u6587\u4ef6\u521b\u5efa\u65f6\u95f4\u7684\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u5bfc\u5165pathlib\u6a21\u5757\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u4f7f\u7528Path\u7c7b\u7684stat()\u65b9\u6cd5\u83b7\u53d6\u6587\u4ef6\u521b\u5efa\u65f6\u95f4\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">file_path = Path(&#39;example.txt&#39;)<\/p>\n<p>creation_time = file_path.stat().st_ctime<\/p>\n<p>print(&quot;\u521b\u5efa\u65f6\u95f4\uff1a&quot;, creation_time)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li>\u5c06\u83b7\u53d6\u5230\u7684\u65f6\u95f4\u8f6c\u6362\u4e3a\u53ef\u8bfb\u683c\u5f0f\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">readable_time = time.strftime(&#39;%Y-%m-%d %H:%M:%S&#39;, time.localtime(creation_time))<\/p>\n<p>print(&quot;\u53ef\u8bfb\u683c\u5f0f\u7684\u521b\u5efa\u65f6\u95f4\uff1a&quot;, readable_time)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0eos\u6a21\u5757\u7c7b\u4f3c\uff0cPathlib\u6a21\u5757\u5728Windows\u7cfb\u7edf\u4e2d\u540c\u6837\u53ef\u4ee5\u76f4\u63a5\u83b7\u5f97\u6587\u4ef6\u7684\u521b\u5efa\u65f6\u95f4\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u8de8\u5e73\u53f0\u6ce8\u610f\u4e8b\u9879<\/p>\n<\/p>\n<p><p>\u5728\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u4e0a\uff0c\u83b7\u53d6\u6587\u4ef6\u521b\u5efa\u65f6\u95f4\u7684\u65b9\u6cd5\u53ef\u80fd\u5b58\u5728\u5dee\u5f02\u3002\u5728Windows\u7cfb\u7edf\u4e0a\uff0cos.path.getctime()\u548cPathlib\u7684st_ctime\u5c5e\u6027\u901a\u5e38\u80fd\u63d0\u4f9b\u51c6\u786e\u7684\u6587\u4ef6\u521b\u5efa\u65f6\u95f4\u3002\u7136\u800c\uff0c\u5728Unix\/Linux\u7cfb\u7edf\u4e2d\uff0c\u8fd9\u4e9b\u65b9\u6cd5\u53ef\u80fd\u8fd4\u56de\u7684\u662f\u6587\u4ef6\u7684\u6700\u540e\u72b6\u6001\u66f4\u6539\u65f6\u95f4\uff0c\u800c\u4e0d\u662f\u521b\u5efa\u65f6\u95f4\u3002\u8fd9\u662f\u56e0\u4e3aUnix\/Linux\u7cfb\u7edf\u901a\u5e38\u4e0d\u8bb0\u5f55\u6587\u4ef6\u7684\u521b\u5efa\u65f6\u95f4\u3002<\/p>\n<\/p>\n<p><p>\u56e0\u6b64\uff0c\u5982\u679c\u9700\u8981\u5728Unix\/Linux\u7cfb\u7edf\u4e0a\u83b7\u53d6\u6587\u4ef6\u7684\u521b\u5efa\u65f6\u95f4\uff0c\u53ef\u4ee5\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\uff0c\u5982pyinotify\uff0c\u5c3d\u7ba1\u5b83\u4eec\u53ef\u80fd\u9700\u8981\u5b89\u88c5\u989d\u5916\u7684\u4f9d\u8d56\u6216\u5177\u6709\u4e00\u5b9a\u7684\u4f7f\u7528\u9650\u5236\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93<\/p>\n<\/p>\n<p><p>\u6709\u65f6\u5019\uff0c\u6807\u51c6\u5e93\u7684\u65b9\u6cd5\u53ef\u80fd\u65e0\u6cd5\u6ee1\u8db3\u6240\u6709\u9700\u6c42\uff0c\u7279\u522b\u662f\u5728\u5904\u7406\u8de8\u5e73\u53f0\u6587\u4ef6\u7cfb\u7edf\u64cd\u4f5c\u65f6\u3002\u8fd9\u65f6\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\uff0c\u5982osxmetadata\uff08\u9002\u7528\u4e8emacOS\uff09\u6216pywin32\uff08\u9002\u7528\u4e8eWindows\uff09\u6765\u83b7\u53d6\u6587\u4ef6\u7684\u521b\u5efa\u65f6\u95f4\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u5728Windows\u5e73\u53f0\u4e0a\uff0c\u53ef\u4ee5\u4f7f\u7528pywin32\u5e93\u4e2d\u7684win32file\u6a21\u5757\u83b7\u53d6\u6587\u4ef6\u521b\u5efa\u65f6\u95f4\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u5b89\u88c5pywin32\u5e93\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-bash\">pip install pywin32<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u4f7f\u7528win32file\u6a21\u5757\u83b7\u53d6\u6587\u4ef6\u521b\u5efa\u65f6\u95f4\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">import win32file<\/p>\n<p>file_path = &#39;example.txt&#39;<\/p>\n<p>handle = win32file.CreateFile(<\/p>\n<p>    file_path,<\/p>\n<p>    win32file.GENERIC_READ,<\/p>\n<p>    win32file.FILE_SHARE_READ,<\/p>\n<p>    None,<\/p>\n<p>    win32file.OPEN_EXISTING,<\/p>\n<p>    0,<\/p>\n<p>    None<\/p>\n<p>)<\/p>\n<p>creation_time = win32file.GetFileTime(handle)[0]<\/p>\n<p>win32file.CloseHandle(handle)<\/p>\n<h2><strong>\u5c06\u83b7\u53d6\u5230\u7684\u65f6\u95f4\u8f6c\u6362\u4e3a\u53ef\u8bfb\u683c\u5f0f<\/strong><\/h2>\n<p>from datetime import datetime<\/p>\n<p>readable_time = datetime.fromtimestamp(creation_time).strftime(&#39;%Y-%m-%d %H:%M:%S&#39;)<\/p>\n<p>print(&quot;\u53ef\u8bfb\u683c\u5f0f\u7684\u521b\u5efa\u65f6\u95f4\uff1a&quot;, readable_time)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e94\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u83b7\u53d6\u6587\u4ef6\u521b\u5efa\u65f6\u95f4\u5728\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u4e0a\u53ef\u80fd\u4f1a\u6709\u4e0d\u540c\u7684\u5b9e\u73b0\u65b9\u5f0f\u3002\u5728Windows\u7cfb\u7edf\u4e0a\uff0cos.path.getctime()\u548cPathlib\u6a21\u5757\u901a\u5e38\u53ef\u4ee5\u76f4\u63a5\u83b7\u53d6\u6587\u4ef6\u521b\u5efa\u65f6\u95f4\u3002\u800c\u5728Unix\/Linux\u7cfb\u7edf\u4e0a\uff0c\u53ef\u80fd\u9700\u8981\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u6216\u4f9d\u8d56\u7cfb\u7edf\u7279\u6027\u6765\u5b9e\u73b0\u3002\u65e0\u8bba\u4f7f\u7528\u54ea\u79cd\u65b9\u6cd5\uff0c\u90fd\u9700\u8981\u6839\u636e\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\u9009\u62e9\u5408\u9002\u7684\u89e3\u51b3\u65b9\u6848\u3002\u4f7f\u7528Python\u6807\u51c6\u5e93\u4e2d\u7684os\u6a21\u5757\u548cpathlib\u6a21\u5757\u901a\u5e38\u662f\u6700\u7b80\u5355\u3001\u6700\u76f4\u63a5\u7684\u65b9\u6cd5\uff0c\u7279\u522b\u662f\u5728Windows\u7cfb\u7edf\u4e0a\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u83b7\u53d6\u6587\u4ef6\u7684\u521b\u5efa\u65f6\u95f4\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>os<\/code>\u6a21\u5757\u548c<code>os.path<\/code>\u6a21\u5757\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\u8be5\u51fd\u6570\u8fd4\u56de\u6587\u4ef6\u7684\u521b\u5efa\u65f6\u95f4\u6233\u3002\u63a5\u4e0b\u6765\uff0c\u53ef\u4ee5\u4f7f\u7528<code>datetime<\/code>\u6a21\u5757\u5c06\u65f6\u95f4\u6233\u8f6c\u6362\u4e3a\u53ef\u8bfb\u7684\u65e5\u671f\u683c\u5f0f\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">import os\nimport datetime\n\nfile_path = &#39;your_file.txt&#39;\ncreation_time = os.path.getctime(file_path)\nreadable_time = datetime.datetime.fromtimestamp(creation_time)\nprint(f&quot;\u6587\u4ef6\u521b\u5efa\u65f6\u95f4: {readable_time}&quot;)\n<\/code><\/pre>\n<p><strong>\u83b7\u53d6\u6587\u4ef6\u521b\u5efa\u65f6\u95f4\u7684\u8de8\u5e73\u53f0\u517c\u5bb9\u6027\u5982\u4f55\uff1f<\/strong><br \/>\u5728\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u4e0a\uff0c\u6587\u4ef6\u7684\u521b\u5efa\u65f6\u95f4\u83b7\u53d6\u65b9\u5f0f\u53ef\u80fd\u4f1a\u6709\u6240\u4e0d\u540c\u3002Windows\u7cfb\u7edf\u901a\u5e38\u652f\u6301\u83b7\u53d6\u6587\u4ef6\u521b\u5efa\u65f6\u95f4\uff0c\u800c\u5728Linux\u548cmacOS\u4e0a\uff0c\u521b\u5efa\u65f6\u95f4\u53ef\u80fd\u5e76\u4e0d\u603b\u662f\u53ef\u7528\u3002\u56e0\u6b64\uff0c\u4f7f\u7528<code>os.path.getctime()<\/code>\u5728Windows\u4e0a\u53ef\u4ee5\u83b7\u5f97\u51c6\u786e\u7684\u521b\u5efa\u65f6\u95f4\uff0c\u4f46\u5728Linux\u4e0a\u53ef\u80fd\u8fd4\u56de\u6700\u540e\u4fee\u6539\u65f6\u95f4\u3002\u5982\u679c\u9700\u8981\u8de8\u5e73\u53f0\u517c\u5bb9\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528<code>stat<\/code>\u6a21\u5757\uff0c\u83b7\u53d6\u66f4\u8be6\u7ec6\u7684\u6587\u4ef6\u4fe1\u606f\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u83b7\u53d6\u6587\u4ef6\u7684\u5176\u4ed6\u65f6\u95f4\u5c5e\u6027\uff1f<\/strong><br \/>\u9664\u4e86\u6587\u4ef6\u521b\u5efa\u65f6\u95f4\uff0c\u60a8\u8fd8\u53ef\u4ee5\u83b7\u53d6\u6587\u4ef6\u7684\u6700\u540e\u4fee\u6539\u65f6\u95f4\u548c\u6700\u540e\u8bbf\u95ee\u65f6\u95f4\u3002\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>os.path.getmtime()<\/code>\u548c<code>os.path.getatime()<\/code>\u51fd\u6570\u5206\u522b\u83b7\u53d6\u8fd9\u4e9b\u4fe1\u606f\u3002\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">modification_time = os.path.getmtime(file_path)\naccess_time = os.path.getatime(file_path)\n\nprint(f&quot;\u6700\u540e\u4fee\u6539\u65f6\u95f4: {datetime.datetime.fromtimestamp(modification_time)}&quot;)\nprint(f&quot;\u6700\u540e\u8bbf\u95ee\u65f6\u95f4: {datetime.datetime.fromtimestamp(access_time)}&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u4e9b\u4fe1\u606f\u5bf9\u4e8e\u6587\u4ef6\u7ba1\u7406\u548c\u5ba1\u8ba1\u90fd\u975e\u5e38\u6709\u7528\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u83b7\u53d6\u6587\u4ef6\u521b\u5efa\u65f6\u95f4\uff0c\u4e3b\u8981\u53ef\u4ee5\u901a\u8fc7os\u6a21\u5757\u3001os.path\u6a21\u5757\u3001\u4ee5\u53ca\u7b2c\u4e09\u65b9\u5e93\u5982pathlib\u6765\u5b9e\u73b0 [&hellip;]","protected":false},"author":3,"featured_media":1011668,"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\/1011651"}],"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=1011651"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1011651\/revisions"}],"predecessor-version":[{"id":1011672,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1011651\/revisions\/1011672"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1011668"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1011651"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1011651"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1011651"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}