{"id":954139,"date":"2024-12-27T01:52:06","date_gmt":"2024-12-26T17:52:06","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/954139.html"},"modified":"2024-12-27T01:52:09","modified_gmt":"2024-12-26T17:52:09","slug":"python%e5%a6%82%e4%bd%95%e9%87%8d%e5%91%bd%e5%90%8d%e6%96%87%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/954139.html","title":{"rendered":"python\u5982\u4f55\u91cd\u547d\u540d\u6587\u4ef6"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25094436\/4283418a-98bd-4bc1-a24b-694a42f1a0d1.webp\" alt=\"python\u5982\u4f55\u91cd\u547d\u540d\u6587\u4ef6\" \/><\/p>\n<p><p> \u5728Python\u4e2d\u91cd\u547d\u540d\u6587\u4ef6\u53ef\u4ee5\u901a\u8fc7<strong>os\u6a21\u5757\u3001shutil\u6a21\u5757\u3001pathlib\u6a21\u5757<\/strong>\u6765\u5b9e\u73b0\u3002\u5176\u4e2d\uff0cos\u6a21\u5757\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5b83\u662fPython\u5185\u7f6e\u7684\u6a21\u5757\uff0c\u9002\u7528\u4e8e\u5927\u591a\u6570\u6587\u4ef6\u64cd\u4f5c\u4efb\u52a1\u3002shutil\u6a21\u5757\u5219\u63d0\u4f9b\u4e86\u66f4\u9ad8\u5c42\u6b21\u7684\u6587\u4ef6\u64cd\u4f5c\u529f\u80fd\uff0c\u800cpathlib\u6a21\u5757\u5219\u63d0\u4f9b\u4e86\u4e00\u79cd\u9762\u5411\u5bf9\u8c61\u7684\u65b9\u6cd5\u6765\u5904\u7406\u6587\u4ef6\u8def\u5f84\u3002<strong>os.rename()\u51fd\u6570\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u4e4b\u4e00<\/strong>\uff0c\u5b83\u7b80\u5355\u76f4\u63a5\u4e14\u6613\u4e8e\u4f7f\u7528\u3002\u4ee5\u4e0b\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u6bcf\u79cd\u65b9\u6cd5\u7684\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528OS\u6a21\u5757\u91cd\u547d\u540d\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>os\u6a21\u5757\u662fPython\u5185\u7f6e\u7684\u6807\u51c6\u5e93\u4e4b\u4e00\uff0c\u4e3b\u8981\u7528\u4e8e\u4e0e\u64cd\u4f5c\u7cfb\u7edf\u8fdb\u884c\u4ea4\u4e92\u3002\u4f7f\u7528os\u6a21\u5757\u91cd\u547d\u540d\u6587\u4ef6\u662f\u4e00\u79cd\u975e\u5e38\u76f4\u63a5\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h4>1. \u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528os\u6a21\u5757\u91cd\u547d\u540d\u6587\u4ef6\u7684\u57fa\u672c\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u6307\u5b9a\u65e7\u6587\u4ef6\u540d\u548c\u65b0\u6587\u4ef6\u540d<\/strong><\/h2>\n<p>old_name = &#39;old_file.txt&#39;<\/p>\n<p>new_name = &#39;new_file.txt&#39;<\/p>\n<h2><strong>\u91cd\u547d\u540d\u6587\u4ef6<\/strong><\/h2>\n<p>os.rename(old_name, new_name)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>os.rename()<\/code>\u51fd\u6570\u7528\u4e8e\u5c06<code>old_file.txt<\/code>\u91cd\u547d\u540d\u4e3a<code>new_file.txt<\/code>\u3002\u5982\u679c\u6587\u4ef6\u540d\u66f4\u6539\u6210\u529f\uff0c\u51fd\u6570\u5c06\u6267\u884c\u800c\u6ca1\u6709\u9519\u8bef\uff1b\u5426\u5219\uff0c\u5c06\u629b\u51faOSError\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><h4>2. \u9519\u8bef\u5904\u7406<\/h4>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u91cd\u547d\u540d\u6587\u4ef6\u65f6\u53ef\u80fd\u4f1a\u9047\u5230\u5404\u79cd\u9519\u8bef\uff0c\u4f8b\u5982\u6587\u4ef6\u4e0d\u5b58\u5728\u3001\u6743\u9650\u4e0d\u8db3\u7b49\u3002\u4e3a\u4e86\u5904\u7406\u8fd9\u4e9b\u60c5\u51b5\uff0c\u901a\u5e38\u4f1a\u4f7f\u7528\u5f02\u5e38\u5904\u7406\u673a\u5236\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>old_name = &#39;old_file.txt&#39;<\/p>\n<p>new_name = &#39;new_file.txt&#39;<\/p>\n<p>try:<\/p>\n<p>    os.rename(old_name, new_name)<\/p>\n<p>    print(f&quot;\u6587\u4ef6\u91cd\u547d\u540d\u6210\u529f: {old_name} -&gt; {new_name}&quot;)<\/p>\n<p>except FileNotFoundError:<\/p>\n<p>    print(f&quot;\u9519\u8bef: \u627e\u4e0d\u5230\u6587\u4ef6 &#39;{old_name}&#39;&quot;)<\/p>\n<p>except PermissionError:<\/p>\n<p>    print(f&quot;\u9519\u8bef: \u65e0\u6cd5\u91cd\u547d\u540d &#39;{old_name}&#39;\uff0c\u6743\u9650\u4e0d\u8db3&quot;)<\/p>\n<p>except Exception as e:<\/p>\n<p>    print(f&quot;\u51fa\u73b0\u9519\u8bef: {e}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u6355\u6349\u5e76\u5904\u7406\u53ef\u80fd\u7684\u5f02\u5e38\uff0c\u786e\u4fdd\u7a0b\u5e8f\u7684\u5065\u58ee\u6027\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528SHUTIL\u6a21\u5757\u91cd\u547d\u540d\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>shutil\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u4e2a\u66f4\u9ad8\u7ea7\u522b\u7684\u63a5\u53e3\u6765\u5904\u7406\u6587\u4ef6\u64cd\u4f5c\uff0c\u5305\u62ec\u590d\u5236\u3001\u79fb\u52a8\u548c\u91cd\u547d\u540d\u7b49\u3002\u867d\u7136os\u6a21\u5757\u5df2\u7ecf\u80fd\u591f\u6ee1\u8db3\u5927\u591a\u6570\u91cd\u547d\u540d\u4efb\u52a1\uff0c\u4f46\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0cshutil\u6a21\u5757\u53ef\u80fd\u66f4\u9002\u5408\u3002<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528shutil.move()<\/h4>\n<\/p>\n<p><p><code>shutil.move()<\/code>\u51fd\u6570\u5b9e\u9645\u4e0a\u662f\u7528\u4e8e\u79fb\u52a8\u6587\u4ef6\u7684\uff0c\u4f46\u5982\u679c\u6e90\u8def\u5f84\u548c\u76ee\u6807\u8def\u5f84\u5728\u540c\u4e00\u76ee\u5f55\u4e0b\uff0c\u5219\u76f8\u5f53\u4e8e\u91cd\u547d\u540d\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import shutil<\/p>\n<p>old_name = &#39;old_file.txt&#39;<\/p>\n<p>new_name = &#39;new_file.txt&#39;<\/p>\n<p>shutil.move(old_name, new_name)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>shutil.move()\u4f1a\u81ea\u52a8\u5904\u7406\u8de8\u6587\u4ef6\u7cfb\u7edf\u7684\u79fb\u52a8\uff0c\u8fd9\u4f7f\u5f97\u5b83\u6bd4os.rename()\u66f4\u52a0\u7075\u6d3b\u3002<\/p>\n<\/p>\n<p><h4>2. \u5e94\u7528\u573a\u666f<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u91cd\u547d\u540d\u7684\u540c\u65f6\u9700\u8981\u6539\u53d8\u6587\u4ef6\u7684\u76ee\u5f55\uff0c\u90a3\u4e48\u4f7f\u7528shutil.move()\u53ef\u80fd\u66f4\u52a0\u65b9\u4fbf\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import shutil<\/p>\n<h2><strong>\u5c06\u6587\u4ef6\u4ece\u4e00\u4e2a\u76ee\u5f55\u91cd\u547d\u540d\u5e76\u79fb\u52a8\u5230\u53e6\u4e00\u4e2a\u76ee\u5f55<\/strong><\/h2>\n<p>shutil.move(&#39;\/path\/to\/old_directory\/old_file.txt&#39;, &#39;\/path\/to\/new_directory\/new_file.txt&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u5728\u9700\u8981\u540c\u65f6\u79fb\u52a8\u548c\u91cd\u547d\u540d\u6587\u4ef6\u65f6\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528PATHLIB\u6a21\u5757\u91cd\u547d\u540d\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>pathlib\u6a21\u5757\u662fPython 3.4\u5f15\u5165\u7684\u4e00\u4e2a\u9762\u5411\u5bf9\u8c61\u7684\u6587\u4ef6\u8def\u5f84\u5e93\u3002\u5b83\u63d0\u4f9b\u4e86\u66f4\u76f4\u89c2\u548c\u4f18\u96c5\u7684\u6587\u4ef6\u8def\u5f84\u64cd\u4f5c\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h4>1. \u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528pathlib\u6a21\u5757\u91cd\u547d\u540d\u6587\u4ef6\u7684\u57fa\u672c\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<h2><strong>\u521b\u5efaPath\u5bf9\u8c61<\/strong><\/h2>\n<p>old_name = Path(&#39;old_file.txt&#39;)<\/p>\n<p>new_name = Path(&#39;new_file.txt&#39;)<\/p>\n<h2><strong>\u91cd\u547d\u540d\u6587\u4ef6<\/strong><\/h2>\n<p>old_name.rename(new_name)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u9996\u5148\u521b\u5efa\u4e86\u4e00\u4e2aPath\u5bf9\u8c61\uff0c\u7136\u540e\u4f7f\u7528<code>rename()<\/code>\u65b9\u6cd5\u91cd\u547d\u540d\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h4>2. \u4f7f\u7528Path\u5bf9\u8c61\u7684\u4f18\u52bf<\/h4>\n<\/p>\n<p><p>\u4e0eos\u6a21\u5757\u76f8\u6bd4\uff0cpathlib\u6a21\u5757\u63d0\u4f9b\u4e86\u66f4\u591a\u7684\u8def\u5f84\u64cd\u4f5c\u529f\u80fd\uff0c\u5e76\u4e14\u4ee3\u7801\u66f4\u52a0\u6e05\u6670\u548c\u6613\u4e8e\u7ef4\u62a4\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u83b7\u53d6\u6587\u4ef6\u7684\u7236\u76ee\u5f55\u3001\u6587\u4ef6\u540d\u548c\u6269\u5c55\u540d\u7b49\u4fe1\u606f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<p>file_path = Path(&#39;\/path\/to\/file.txt&#39;)<\/p>\n<h2><strong>\u83b7\u53d6\u6587\u4ef6\u540d<\/strong><\/h2>\n<p>print(file_path.name)  # \u8f93\u51fa: file.txt<\/p>\n<h2><strong>\u83b7\u53d6\u7236\u76ee\u5f55<\/strong><\/h2>\n<p>print(file_path.parent)  # \u8f93\u51fa: \/path\/to<\/p>\n<h2><strong>\u83b7\u53d6\u6587\u4ef6\u6269\u5c55\u540d<\/strong><\/h2>\n<p>print(file_path.suffix)  # \u8f93\u51fa: .txt<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u4f7f\u5f97\u8def\u5f84\u64cd\u4f5c\u66f4\u52a0\u76f4\u89c2\u548c\u7b80\u6d01\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u5728\u9009\u62e9\u5177\u4f53\u7684\u65b9\u6cd5\u6765\u91cd\u547d\u540d\u6587\u4ef6\u65f6\uff0c\u5e94\u8be5\u6839\u636e\u5177\u4f53\u7684\u9700\u6c42\u548c\u5e94\u7528\u573a\u666f\u6765\u51b3\u5b9a\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>os\u6a21\u5757<\/strong>\uff1a\u9002\u7528\u4e8e\u7b80\u5355\u7684\u91cd\u547d\u540d\u4efb\u52a1\uff0c\u5c24\u5176\u662f\u5f53\u53ea\u9700\u8981\u5728\u540c\u4e00\u76ee\u5f55\u4e0b\u91cd\u547d\u540d\u6587\u4ef6\u65f6\u3002<\/li>\n<li><strong>shutil\u6a21\u5757<\/strong>\uff1a\u9002\u7528\u4e8e\u9700\u8981\u540c\u65f6\u79fb\u52a8\u548c\u91cd\u547d\u540d\u6587\u4ef6\uff0c\u6216\u8005\u53ef\u80fd\u8de8\u6587\u4ef6\u7cfb\u7edf\u7684\u573a\u666f\u3002<\/li>\n<li><strong>pathlib\u6a21\u5757<\/strong>\uff1a\u9002\u7528\u4e8e\u9700\u8981\u8fdb\u884c\u590d\u6742\u8def\u5f84\u64cd\u4f5c\u7684\u573a\u5408\uff0c\u5c24\u5176\u662f\u5728Python 3\u73af\u5883\u4e0b\u8fdb\u884c\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u65f6\u3002<\/li>\n<\/ul>\n<p><p>\u7efc\u4e0a\u6240\u8ff0\uff0cPython\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u5b9e\u73b0\u6587\u4ef6\u91cd\u547d\u540d\uff0c\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u72ec\u7279\u7684\u4f18\u52bf\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\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\u91cd\u547d\u540d\u6587\u4ef6\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>os<\/code>\u6a21\u5757\u6765\u91cd\u547d\u540d\u6587\u4ef6\u3002\u53ef\u4ee5\u901a\u8fc7<code>os.rename(old_name, new_name)<\/code>\u51fd\u6570\u5c06\u6587\u4ef6\u7684\u540d\u79f0\u4ece<code>old_name<\/code>\u66f4\u6539\u4e3a<code>new_name<\/code>\u3002\u786e\u4fdd\u5728\u6267\u884c\u6b64\u64cd\u4f5c\u4e4b\u524d\uff0c\u6587\u4ef6\u8def\u5f84\u662f\u6b63\u786e\u7684\uff0c\u5e76\u4e14\u65b0\u540d\u79f0\u4e0d\u4f1a\u4e0e\u73b0\u6709\u6587\u4ef6\u51b2\u7a81\u3002<\/p>\n<p><strong>\u91cd\u547d\u540d\u6587\u4ef6\u65f6\u9700\u8981\u6ce8\u610f\u54ea\u4e9b\u4e8b\u9879\uff1f<\/strong><br \/>\u5728\u91cd\u547d\u540d\u6587\u4ef6\u65f6\uff0c\u6709\u51e0\u4e2a\u5173\u952e\u56e0\u7d20\u9700\u8981\u8003\u8651\u3002\u9996\u5148\uff0c\u786e\u4fdd\u65b0\u6587\u4ef6\u540d\u7b26\u5408\u64cd\u4f5c\u7cfb\u7edf\u7684\u547d\u540d\u89c4\u5219\uff0c\u4f8b\u5982\u907f\u514d\u4f7f\u7528\u7279\u6b8a\u5b57\u7b26\u6216\u4fdd\u7559\u5b57\u3002\u5176\u6b21\uff0c\u786e\u8ba4\u76ee\u6807\u6587\u4ef6\u5939\u5177\u6709\u5199\u5165\u6743\u9650\uff0c\u4ee5\u514d\u51fa\u73b0\u6743\u9650\u9519\u8bef\u3002\u6b64\u5916\uff0c\u91cd\u547d\u540d\u7684\u6587\u4ef6\u4e0d\u5e94\u4e0e\u540c\u4e00\u76ee\u5f55\u4e2d\u5df2\u5b58\u5728\u7684\u6587\u4ef6\u540d\u79f0\u91cd\u590d\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5904\u7406\u91cd\u547d\u540d\u9519\u8bef\uff1f<\/strong><br \/>\u5f53\u5c1d\u8bd5\u91cd\u547d\u540d\u6587\u4ef6\u65f6\uff0c\u53ef\u80fd\u4f1a\u51fa\u73b0\u5404\u79cd\u9519\u8bef\uff0c\u4f8b\u5982\u6587\u4ef6\u4e0d\u5b58\u5728\u3001\u6743\u9650\u4e0d\u8db3\u6216\u76ee\u6807\u6587\u4ef6\u540d\u5df2\u5b58\u5728\u3002\u53ef\u4ee5\u4f7f\u7528<code>try<\/code>\u548c<code>except<\/code>\u8bed\u53e5\u6765\u6355\u83b7\u8fd9\u4e9b\u5f02\u5e38\uff0c\u4ee5\u4fbf\u7f16\u5199\u66f4\u7a33\u5065\u7684\u4ee3\u7801\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u6355\u83b7<code>FileNotFoundError<\/code>\u548c<code>PermissionError<\/code>\uff0c\u5e76\u63d0\u4f9b\u7528\u6237\u53cb\u597d\u7684\u9519\u8bef\u4fe1\u606f\uff0c\u6307\u5bfc\u7528\u6237\u91c7\u53d6\u76f8\u5e94\u7684\u63aa\u65bd\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u91cd\u547d\u540d\u6587\u4ef6\u53ef\u4ee5\u901a\u8fc7os\u6a21\u5757\u3001shutil\u6a21\u5757\u3001pathlib\u6a21\u5757\u6765\u5b9e\u73b0\u3002\u5176\u4e2d\uff0cos\u6a21\u5757\u662f\u6700\u5e38\u7528 [&hellip;]","protected":false},"author":3,"featured_media":954146,"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\/954139"}],"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=954139"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/954139\/revisions"}],"predecessor-version":[{"id":954148,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/954139\/revisions\/954148"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/954146"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=954139"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=954139"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=954139"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}