{"id":1078869,"date":"2025-01-08T12:15:35","date_gmt":"2025-01-08T04:15:35","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1078869.html"},"modified":"2025-01-08T12:15:37","modified_gmt":"2025-01-08T04:15:37","slug":"python%e5%a6%82%e4%bd%95%e6%89%b9%e9%87%8f%e9%87%8d%e5%91%bd%e5%90%8d%e6%96%87%e4%bb%b6%e5%a4%b9-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1078869.html","title":{"rendered":"python\u5982\u4f55\u6279\u91cf\u91cd\u547d\u540d\u6587\u4ef6\u5939"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24182209\/1d1b0024-a091-4424-a852-b38052435cf0.webp\" alt=\"python\u5982\u4f55\u6279\u91cf\u91cd\u547d\u540d\u6587\u4ef6\u5939\" \/><\/p>\n<p><p> <strong>\u4f7f\u7528Python\u6279\u91cf\u91cd\u547d\u540d\u6587\u4ef6\u5939\u7684\u6b65\u9aa4\u5305\u62ec\uff1a\u5bfc\u5165\u5fc5\u8981\u7684\u5e93\u3001\u83b7\u53d6\u6587\u4ef6\u5939\u5217\u8868\u3001\u904d\u5386\u6587\u4ef6\u5939\u5e76\u91cd\u547d\u540d\u3002<\/strong> \u5176\u4e2d\uff0cos\u6a21\u5757\u662f\u5fc5\u4e0d\u53ef\u5c11\u7684\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u8bbf\u95ee\u64cd\u4f5c\u7cfb\u7edf\u529f\u80fd\u7684\u65b9\u6cd5\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u6df1\u5165\u63a2\u8ba8\u5982\u4f55\u4f7f\u7528Python\u6279\u91cf\u91cd\u547d\u540d\u6587\u4ef6\u5939\u3002<\/p>\n<\/p>\n<hr>\n<h2><strong>\u4e00\u3001\u5bfc\u5165\u5fc5\u8981\u7684\u5e93<\/strong><\/h2>\n<p><p>\u5728Python\u4e2d\uff0cos\u6a21\u5757\u548cshutil\u6a21\u5757\u662f\u5904\u7406\u6587\u4ef6\u548c\u76ee\u5f55\u64cd\u4f5c\u7684\u4e3b\u8981\u5de5\u5177\u3002\u9996\u5148\uff0c\u5bfc\u5165\u8fd9\u4e9b\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>import shutil<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>os\u5e93<\/strong>\uff1a\u7528\u4e8e\u4e0e\u64cd\u4f5c\u7cfb\u7edf\u8fdb\u884c\u4ea4\u4e92\uff0c\u5305\u62ec\u6587\u4ef6\u548c\u76ee\u5f55\u7684\u521b\u5efa\u3001\u5220\u9664\u3001\u91cd\u547d\u540d\u7b49\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><p><strong>shutil\u5e93<\/strong>\uff1a\u63d0\u4f9b\u9ad8\u7ea7\u6587\u4ef6\u64cd\u4f5c\uff0c\u5982\u590d\u5236\u3001\u79fb\u52a8\u548c\u5220\u9664\u6587\u4ef6\u4ee5\u53ca\u76ee\u5f55\u3002<\/p>\n<\/p>\n<hr>\n<h2><strong>\u4e8c\u3001\u83b7\u53d6\u6587\u4ef6\u5939\u5217\u8868<\/strong><\/h2>\n<p><p>\u4e3a\u4e86\u6279\u91cf\u91cd\u547d\u540d\u6587\u4ef6\u5939\uff0c\u9996\u5148\u9700\u8981\u83b7\u53d6\u76ee\u6807\u76ee\u5f55\u4e2d\u6240\u6709\u6587\u4ef6\u5939\u7684\u5217\u8868\u3002\u8fd9\u53ef\u4ee5\u4f7f\u7528os.listdir()\u51fd\u6570\u6765\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">directory = &quot;\/path\/to\/your\/directory&quot;<\/p>\n<p>folders = [f for f in os.listdir(directory) if os.path.isdir(os.path.join(directory, f))]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u5c06\u5217\u51fa\u6307\u5b9a\u76ee\u5f55\u4e2d\u7684\u6240\u6709\u6587\u4ef6\u5939\uff0c\u5e76\u5c06\u5b83\u4eec\u5b58\u50a8\u5728\u4e00\u4e2a\u5217\u8868\u4e2d\u3002<\/p>\n<\/p>\n<hr>\n<h2><strong>\u4e09\u3001\u904d\u5386\u6587\u4ef6\u5939\u5e76\u91cd\u547d\u540d<\/strong><\/h2>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u904d\u5386\u6587\u4ef6\u5939\u5217\u8868\u5e76\u4f7f\u7528os.rename()\u51fd\u6570\u91cd\u547d\u540d\u6bcf\u4e2a\u6587\u4ef6\u5939\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for folder in folders:<\/p>\n<p>    old_folder_path = os.path.join(directory, folder)<\/p>\n<p>    new_folder_name = &quot;new_prefix_&quot; + folder<\/p>\n<p>    new_folder_path = os.path.join(directory, new_folder_name)<\/p>\n<p>    os.rename(old_folder_path, new_folder_path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4e3a\u6bcf\u4e2a\u6587\u4ef6\u5939\u6dfb\u52a0\u4e86\u4e00\u4e2a\u524d\u7f00\u201cnew_prefix_\u201d\u3002\u4f60\u53ef\u4ee5\u6839\u636e\u9700\u8981\u4fee\u6539\u65b0\u6587\u4ef6\u5939\u7684\u547d\u540d\u89c4\u5219\u3002<\/p>\n<\/p>\n<hr>\n<h2><strong>\u56db\u3001\u5904\u7406\u91cd\u547d\u540d\u4e2d\u7684\u7279\u6b8a\u60c5\u51b5<\/strong><\/h2>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u4e00\u4e9b\u7279\u6b8a\u60c5\u51b5\uff0c\u4f8b\u5982\u6587\u4ef6\u5939\u540d\u4e2d\u5305\u542b\u7a7a\u683c\u6216\u7279\u6b8a\u5b57\u7b26\u3002\u4e3a\u4e86\u786e\u4fdd\u4ee3\u7801\u7684\u5065\u58ee\u6027\uff0c\u53ef\u4ee5\u5728\u91cd\u547d\u540d\u524d\u8fdb\u884c\u4e00\u4e9b\u9884\u5904\u7406\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def sanitize_folder_name(name):<\/p>\n<p>    return name.replace(&quot; &quot;, &quot;_&quot;).replace(&quot;&amp;&quot;, &quot;and&quot;)<\/p>\n<p>for folder in folders:<\/p>\n<p>    old_folder_path = os.path.join(directory, folder)<\/p>\n<p>    sanitized_folder_name = sanitize_folder_name(folder)<\/p>\n<p>    new_folder_name = &quot;new_prefix_&quot; + sanitized_folder_name<\/p>\n<p>    new_folder_path = os.path.join(directory, new_folder_name)<\/p>\n<p>    os.rename(old_folder_path, new_folder_path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u793a\u4f8b\u4e2d\u7684sanitize_folder_name\u51fd\u6570\u5c06\u6587\u4ef6\u5939\u540d\u79f0\u4e2d\u7684\u7a7a\u683c\u66ff\u6362\u4e3a\u4e0b\u5212\u7ebf\uff0c\u5e76\u5c06\u201c&amp;\u201d\u66ff\u6362\u4e3a\u201cand\u201d\u3002<\/p>\n<\/p>\n<hr>\n<h2><strong>\u4e94\u3001\u8bb0\u5f55\u91cd\u547d\u540d\u64cd\u4f5c<\/strong><\/h2>\n<p><p>\u4e3a\u4e86\u786e\u4fdd\u91cd\u547d\u540d\u64cd\u4f5c\u7684\u53ef\u8ffd\u6eaf\u6027\uff0c\u53ef\u4ee5\u5c06\u65e7\u540d\u79f0\u548c\u65b0\u540d\u79f0\u8bb0\u5f55\u5230\u4e00\u4e2a\u65e5\u5fd7\u6587\u4ef6\u4e2d\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">log_file = &quot;rename_log.txt&quot;<\/p>\n<p>with open(log_file, &quot;w&quot;) as log:<\/p>\n<p>    for folder in folders:<\/p>\n<p>        old_folder_path = os.path.join(directory, folder)<\/p>\n<p>        new_folder_name = &quot;new_prefix_&quot; + folder<\/p>\n<p>        new_folder_path = os.path.join(directory, new_folder_name)<\/p>\n<p>        os.rename(old_folder_path, new_folder_path)<\/p>\n<p>        log.write(f&quot;Renamed: {folder} -&gt; {new_folder_name}\\n&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6837\uff0c\u5982\u679c\u9700\u8981\u56de\u6eaf\u6216\u68c0\u67e5\u91cd\u547d\u540d\u64cd\u4f5c\uff0c\u53ef\u4ee5\u901a\u8fc7\u65e5\u5fd7\u6587\u4ef6\u8fdb\u884c\u8ffd\u8e2a\u3002<\/p>\n<\/p>\n<hr>\n<h2><strong>\u516d\u3001\u5904\u7406\u9519\u8bef\u548c\u5f02\u5e38<\/strong><\/h2>\n<p><p>\u5728\u6279\u91cf\u64cd\u4f5c\u4e2d\uff0c\u5904\u7406\u9519\u8bef\u548c\u5f02\u5e38\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u53ef\u4ee5\u4f7f\u7528try-except\u5757\u6765\u6355\u83b7\u548c\u5904\u7406\u53ef\u80fd\u53d1\u751f\u7684\u9519\u8bef\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for folder in folders:<\/p>\n<p>    old_folder_path = os.path.join(directory, folder)<\/p>\n<p>    new_folder_name = &quot;new_prefix_&quot; + folder<\/p>\n<p>    new_folder_path = os.path.join(directory, new_folder_name)<\/p>\n<p>    try:<\/p>\n<p>        os.rename(old_folder_path, new_folder_path)<\/p>\n<p>        print(f&quot;Renamed: {folder} -&gt; {new_folder_name}&quot;)<\/p>\n<p>    except Exception as e:<\/p>\n<p>        print(f&quot;Error renaming {folder}: {e}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u5982\u679c\u91cd\u547d\u540d\u64cd\u4f5c\u5931\u8d25\uff0c\u9519\u8bef\u4fe1\u606f\u5c06\u88ab\u6355\u83b7\u5e76\u6253\u5370\u51fa\u6765\u3002<\/p>\n<\/p>\n<hr>\n<h2><strong>\u4e03\u3001\u7efc\u5408\u793a\u4f8b<\/strong><\/h2>\n<p><p>\u5c06\u4ee5\u4e0a\u6240\u6709\u6b65\u9aa4\u7efc\u5408\u5728\u4e00\u8d77\uff0c\u53ef\u4ee5\u5f97\u5230\u4e00\u4e2a\u5b8c\u6574\u7684\u6279\u91cf\u91cd\u547d\u540d\u6587\u4ef6\u5939\u7684Python\u811a\u672c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>def sanitize_folder_name(name):<\/p>\n<p>    return name.replace(&quot; &quot;, &quot;_&quot;).replace(&quot;&amp;&quot;, &quot;and&quot;)<\/p>\n<p>def rename_folders(directory, prefix):<\/p>\n<p>    folders = [f for f in os.listdir(directory) if os.path.isdir(os.path.join(directory, f))]<\/p>\n<p>    log_file = &quot;rename_log.txt&quot;<\/p>\n<p>    with open(log_file, &quot;w&quot;) as log:<\/p>\n<p>        for folder in folders:<\/p>\n<p>            old_folder_path = os.path.join(directory, folder)<\/p>\n<p>            sanitized_folder_name = sanitize_folder_name(folder)<\/p>\n<p>            new_folder_name = prefix + sanitized_folder_name<\/p>\n<p>            new_folder_path = os.path.join(directory, new_folder_name)<\/p>\n<p>            try:<\/p>\n<p>                os.rename(old_folder_path, new_folder_path)<\/p>\n<p>                log.write(f&quot;Renamed: {folder} -&gt; {new_folder_name}\\n&quot;)<\/p>\n<p>                print(f&quot;Renamed: {folder} -&gt; {new_folder_name}&quot;)<\/p>\n<p>            except Exception as e:<\/p>\n<p>                log.write(f&quot;Error renaming {folder}: {e}\\n&quot;)<\/p>\n<p>                print(f&quot;Error renaming {folder}: {e}&quot;)<\/p>\n<p>directory = &quot;\/path\/to\/your\/directory&quot;<\/p>\n<p>prefix = &quot;new_prefix_&quot;<\/p>\n<p>rename_folders(directory, prefix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u811a\u672c\u5c06\u76ee\u6807\u76ee\u5f55\u4e2d\u6240\u6709\u6587\u4ef6\u5939\u7684\u540d\u79f0\u524d\u6dfb\u52a0\u4e00\u4e2a\u524d\u7f00\uff0c\u5e76\u8bb0\u5f55\u91cd\u547d\u540d\u64cd\u4f5c\u7684\u65e5\u5fd7\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u5b9e\u73b0\u6587\u4ef6\u5939\u7684\u6279\u91cf\u91cd\u547d\u540d\uff0c\u540c\u65f6\u786e\u4fdd\u64cd\u4f5c\u7684\u53ef\u8ffd\u6eaf\u6027\u548c\u5065\u58ee\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u6279\u91cf\u91cd\u547d\u540d\u591a\u4e2a\u6587\u4ef6\u5939\uff1f<\/strong><br \/>\u4f7f\u7528Python\u8fdb\u884c\u6279\u91cf\u91cd\u547d\u540d\u6587\u4ef6\u5939\u53ef\u4ee5\u901a\u8fc7<code>os<\/code>\u6a21\u5757\u6765\u5b9e\u73b0\u3002\u9996\u5148\uff0c\u60a8\u9700\u8981\u5bfc\u5165<code>os<\/code>\u6a21\u5757\uff0c\u7136\u540e\u4f7f\u7528<code>os.rename()<\/code>\u51fd\u6570\u5c06\u6bcf\u4e2a\u6587\u4ef6\u5939\u7684\u540d\u79f0\u66f4\u6539\u4e3a\u60a8\u6240\u5e0c\u671b\u7684\u65b0\u540d\u79f0\u3002\u53ef\u4ee5\u4f7f\u7528\u5faa\u73af\u904d\u5386\u6587\u4ef6\u5939\u5217\u8868\uff0c\u7ed3\u5408\u5b57\u7b26\u4e32\u64cd\u4f5c\u6765\u5b9a\u4e49\u65b0\u7684\u6587\u4ef6\u5939\u540d\u79f0\u3002<\/p>\n<p><strong>\u5728\u6279\u91cf\u91cd\u547d\u540d\u6587\u4ef6\u5939\u65f6\uff0c\u6709\u54ea\u4e9b\u6ce8\u610f\u4e8b\u9879\uff1f<\/strong><br \/>\u5728\u8fdb\u884c\u6279\u91cf\u91cd\u547d\u540d\u65f6\uff0c\u786e\u4fdd\u65b0\u540d\u79f0\u4e0d\u4f1a\u4e0e\u73b0\u6709\u6587\u4ef6\u5939\u540d\u79f0\u51b2\u7a81\u3002\u6b64\u5916\uff0c\u5efa\u8bae\u5728\u6267\u884c\u91cd\u547d\u540d\u64cd\u4f5c\u4e4b\u524d\u5907\u4efd\u6587\u4ef6\u5939\uff0c\u4ee5\u9632\u51fa\u73b0\u610f\u5916\u60c5\u51b5\u3002\u8fd8\u8981\u6ce8\u610f\u6587\u4ef6\u5939\u8def\u5f84\u7684\u6b63\u786e\u6027\uff0c\u786e\u4fdd\u811a\u672c\u5177\u6709\u76f8\u5e94\u7684\u6743\u9650\u6765\u4fee\u6539\u6587\u4ef6\u5939\u540d\u79f0\u3002<\/p>\n<p><strong>Python\u4e2d\u6709\u6ca1\u6709\u5e93\u53ef\u4ee5\u7b80\u5316\u6279\u91cf\u91cd\u547d\u540d\u6587\u4ef6\u5939\u7684\u8fc7\u7a0b\uff1f<\/strong><br \/>\u662f\u7684\uff0c\u9664\u4e86\u4f7f\u7528<code>os<\/code>\u6a21\u5757\uff0c\u60a8\u8fd8\u53ef\u4ee5\u8003\u8651\u4f7f\u7528<code>pathlib<\/code>\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86\u66f4\u9ad8\u7ea7\u548c\u9762\u5411\u5bf9\u8c61\u7684\u6587\u4ef6\u7cfb\u7edf\u8def\u5f84\u64cd\u4f5c\u529f\u80fd\u3002<code>pathlib<\/code>\u53ef\u4ee5\u8ba9\u60a8\u8f7b\u677e\u5730\u904d\u5386\u76ee\u5f55\uff0c\u5e76\u91cd\u547d\u540d\u6587\u4ef6\u5939\uff0c\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u6613\u8bfb\u3002\u6b64\u5916\uff0c\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u5982<code>pyrename<\/code>\u4e5f\u53ef\u4ee5\u66f4\u65b9\u4fbf\u5730\u8fdb\u884c\u6279\u91cf\u91cd\u547d\u540d\u64cd\u4f5c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4f7f\u7528Python\u6279\u91cf\u91cd\u547d\u540d\u6587\u4ef6\u5939\u7684\u6b65\u9aa4\u5305\u62ec\uff1a\u5bfc\u5165\u5fc5\u8981\u7684\u5e93\u3001\u83b7\u53d6\u6587\u4ef6\u5939\u5217\u8868\u3001\u904d\u5386\u6587\u4ef6\u5939\u5e76\u91cd\u547d\u540d\u3002 \u5176\u4e2d\uff0cos\u6a21\u5757 [&hellip;]","protected":false},"author":3,"featured_media":1078870,"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\/1078869"}],"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=1078869"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1078869\/revisions"}],"predecessor-version":[{"id":1078871,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1078869\/revisions\/1078871"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1078870"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1078869"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1078869"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1078869"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}