{"id":1184740,"date":"2025-01-15T19:30:12","date_gmt":"2025-01-15T11:30:12","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1184740.html"},"modified":"2025-01-15T19:30:16","modified_gmt":"2025-01-15T11:30:16","slug":"python%e5%a6%82%e4%bd%95%e6%89%a9%e5%b1%95%e6%96%87%e4%bb%b6%e5%90%8d","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1184740.html","title":{"rendered":"python\u5982\u4f55\u6269\u5c55\u6587\u4ef6\u540d"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25134108\/0b5ab88f-532a-4265-b0dc-d94169a879ee.webp\" alt=\"python\u5982\u4f55\u6269\u5c55\u6587\u4ef6\u540d\" \/><\/p>\n<p><p> \u5728Python\u4e2d\uff0c\u6269\u5c55\u6587\u4ef6\u540d\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u6765\u5b9e\u73b0\uff0c\u5982\u4f7f\u7528\u5b57\u7b26\u4e32\u64cd\u4f5c\u3001os\u6a21\u5757\u3001pathlib\u6a21\u5757\u7b49\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u65b9\u6cd5\u548c\u8be6\u7ec6\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528\u5b57\u7b26\u4e32\u64cd\u4f5c\u3001os\u6a21\u5757\u3001pathlib\u6a21\u5757\u7b49\u65b9\u6cd5\u53ef\u4ee5\u6269\u5c55\u6587\u4ef6\u540d<\/strong>\u3002\u5176\u4e2d\uff0c<strong>\u4f7f\u7528pathlib\u6a21\u5757<\/strong>\u662f\u6211\u63a8\u8350\u7684\u65b9\u5f0f\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u66f4\u73b0\u4ee3\u548c\u76f4\u89c2\u7684\u63a5\u53e3\uff0c\u6613\u4e8e\u9605\u8bfb\u548c\u7ef4\u62a4\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u5b57\u7b26\u4e32\u64cd\u4f5c<\/h3>\n<\/p>\n<p><p>\u5b57\u7b26\u4e32\u64cd\u4f5c\u662f\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u4e4b\u4e00\uff0c\u9002\u7528\u4e8e\u4e00\u4e9b\u7b80\u5355\u7684\u6587\u4ef6\u540d\u6269\u5c55\u9700\u6c42\uff0c\u4f46\u53ef\u80fd\u4e0d\u9002\u7528\u4e8e\u66f4\u590d\u6742\u7684\u8def\u5f84\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">filename = &quot;example.txt&quot;<\/p>\n<p>new_extension = &quot;.bak&quot;<\/p>\n<p>new_filename = filename + new_extension<\/p>\n<p>print(new_filename)  # \u8f93\u51fa: example.txt.bak<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u7b80\u5355\u5730\u5c06\u65b0\u7684\u6269\u5c55\u540d\u8ffd\u52a0\u5230\u539f\u6587\u4ef6\u540d\u7684\u672b\u5c3e\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u7b80\u5355\u7684\u6587\u4ef6\u540d\u64cd\u4f5c\uff0c\u4f46\u5982\u679c\u6587\u4ef6\u8def\u5f84\u6bd4\u8f83\u590d\u6742\uff0c\u53ef\u80fd\u4f1a\u51fa\u9519\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528os\u6a21\u5757<\/h3>\n<\/p>\n<p><p>os\u6a21\u5757\u63d0\u4f9b\u4e86\u66f4\u5f3a\u5927\u7684\u6587\u4ef6\u548c\u76ee\u5f55\u64cd\u4f5c\u529f\u80fd\uff0c\u53ef\u4ee5\u66f4\u53ef\u9760\u5730\u5904\u7406\u6587\u4ef6\u540d\u548c\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>filename = &quot;example.txt&quot;<\/p>\n<p>new_extension = &quot;.bak&quot;<\/p>\n<p>root, ext = os.path.splitext(filename)<\/p>\n<p>new_filename = root + ext + new_extension<\/p>\n<p>print(new_filename)  # \u8f93\u51fa: example.txt.bak<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0cos.path.splitext()\u51fd\u6570\u5c06\u6587\u4ef6\u540d\u5206\u5272\u6210\u6839\u90e8\u5206\u548c\u6269\u5c55\u540d\u90e8\u5206\uff0c\u7136\u540e\u5c06\u65b0\u7684\u6269\u5c55\u540d\u8ffd\u52a0\u5230\u6839\u90e8\u5206\u548c\u539f\u59cb\u6269\u5c55\u540d\u4e4b\u540e\u3002\u8fd9\u79cd\u65b9\u6cd5\u6bd4\u7b80\u5355\u7684\u5b57\u7b26\u4e32\u64cd\u4f5c\u66f4\u53ef\u9760\uff0c\u56e0\u4e3a\u5b83\u80fd\u6b63\u786e\u5904\u7406\u5e26\u6709\u591a\u4e2a\u70b9\u7684\u6587\u4ef6\u540d\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528pathlib\u6a21\u5757<\/h3>\n<\/p>\n<p><p>pathlib\u6a21\u5757\u662fPython 3.4\u5f15\u5165\u7684\u4e00\u4e2a\u73b0\u4ee3\u5316\u7684\u8def\u5f84\u64cd\u4f5c\u5e93\uff0c\u63d0\u4f9b\u4e86\u66f4\u76f4\u89c2\u548c\u9762\u5411\u5bf9\u8c61\u7684\u63a5\u53e3\u3002\u63a8\u8350\u4f7f\u7528\u8fd9\u4e2a\u6a21\u5757\u6765\u64cd\u4f5c\u6587\u4ef6\u548c\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<p>filename = Path(&quot;example.txt&quot;)<\/p>\n<p>new_extension = &quot;.bak&quot;<\/p>\n<p>new_filename = filename.with_suffix(filename.suffix + new_extension)<\/p>\n<p>print(new_filename)  # \u8f93\u51fa: example.txt.bak<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0cPath\u5bf9\u8c61\u7684with_suffix()\u65b9\u6cd5\u7528\u4e8e\u66ff\u6362\u6587\u4ef6\u7684\u6269\u5c55\u540d\u3002\u6211\u4eec\u5c06\u539f\u59cb\u6269\u5c55\u540d\u548c\u65b0\u7684\u6269\u5c55\u540d\u8fde\u63a5\u5728\u4e00\u8d77\uff0c\u7136\u540e\u8c03\u7528with_suffix()\u65b9\u6cd5\u751f\u6210\u65b0\u7684\u6587\u4ef6\u540d\u3002\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4ec5\u7b80\u6d01\uff0c\u800c\u4e14\u66f4\u6613\u8bfb\u548c\u7ef4\u62a4\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5904\u7406\u590d\u6742\u6587\u4ef6\u8def\u5f84<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6587\u4ef6\u8def\u5f84\u53ef\u80fd\u4f1a\u66f4\u52a0\u590d\u6742\uff0c\u5305\u542b\u76ee\u5f55\u548c\u5b50\u76ee\u5f55\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u4f7f\u7528pathlib\u6a21\u5757\u53ef\u4ee5\u66f4\u65b9\u4fbf\u5730\u5904\u7406\u8def\u5f84\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<p>filepath = Path(&quot;\/home\/user\/documents\/example.txt&quot;)<\/p>\n<p>new_extension = &quot;.bak&quot;<\/p>\n<p>new_filepath = filepath.with_suffix(filepath.suffix + new_extension)<\/p>\n<p>print(new_filepath)  # \u8f93\u51fa: \/home\/user\/documents\/example.txt.bak<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5904\u7406\u4e86\u4e00\u4e2a\u5305\u542b\u5b8c\u6574\u8def\u5f84\u7684\u6587\u4ef6\u540d\u3002pathlib\u6a21\u5757\u80fd\u591f\u81ea\u52a8\u5904\u7406\u8def\u5f84\u4e2d\u7684\u76ee\u5f55\u548c\u5b50\u76ee\u5f55\uff0c\u751f\u6210\u65b0\u7684\u6587\u4ef6\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u6279\u91cf\u5904\u7406\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u6279\u91cf\u5904\u7406\u591a\u4e2a\u6587\u4ef6\uff0c\u6269\u5c55\u5b83\u4eec\u7684\u6587\u4ef6\u540d\u3002\u53ef\u4ee5\u7ed3\u5408os\u6a21\u5757\u548cpathlib\u6a21\u5757\u6765\u5b9e\u73b0\u6279\u91cf\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<p>directory = Path(&quot;\/home\/user\/documents&quot;)<\/p>\n<p>new_extension = &quot;.bak&quot;<\/p>\n<h2><strong>\u6279\u91cf\u5904\u7406\u76ee\u5f55\u4e2d\u7684\u6240\u6709\u6587\u4ef6<\/strong><\/h2>\n<p>for filepath in directory.iterdir():<\/p>\n<p>    if filepath.is_file():<\/p>\n<p>        new_filepath = filepath.with_suffix(filepath.suffix + new_extension)<\/p>\n<p>        print(new_filepath)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u904d\u5386\u6307\u5b9a\u76ee\u5f55\u4e2d\u7684\u6240\u6709\u6587\u4ef6\uff0c\u5e76\u6269\u5c55\u5b83\u4eec\u7684\u6587\u4ef6\u540d\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u6279\u91cf\u5904\u7406\u591a\u4e2a\u6587\u4ef6\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u5904\u7406\u4e0d\u540c\u7c7b\u578b\u7684\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\uff0c\u6211\u4eec\u53ef\u80fd\u53ea\u9700\u8981\u6269\u5c55\u7279\u5b9a\u7c7b\u578b\u6587\u4ef6\u7684\u6587\u4ef6\u540d\u3002\u53ef\u4ee5\u5728\u904d\u5386\u6587\u4ef6\u65f6\u8fdb\u884c\u8fc7\u6ee4\uff0c\u53ea\u5904\u7406\u7279\u5b9a\u6269\u5c55\u540d\u7684\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<p>directory = Path(&quot;\/home\/user\/documents&quot;)<\/p>\n<p>new_extension = &quot;.bak&quot;<\/p>\n<p>target_extension = &quot;.txt&quot;<\/p>\n<h2><strong>\u6279\u91cf\u5904\u7406\u76ee\u5f55\u4e2d\u7684\u6240\u6709.txt\u6587\u4ef6<\/strong><\/h2>\n<p>for filepath in directory.iterdir():<\/p>\n<p>    if filepath.is_file() and filepath.suffix == target_extension:<\/p>\n<p>        new_filepath = filepath.with_suffix(filepath.suffix + new_extension)<\/p>\n<p>        print(new_filepath)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u53ea\u5904\u7406\u6269\u5c55\u540d\u4e3a\u201c.txt\u201d\u7684\u6587\u4ef6\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u5904\u7406\u7279\u5b9a\u7c7b\u578b\u6587\u4ef6\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u5904\u7406\u9012\u5f52\u76ee\u5f55<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u9700\u8981\u9012\u5f52\u5730\u904d\u5386\u76ee\u5f55\u53ca\u5176\u5b50\u76ee\u5f55\uff0c\u6269\u5c55\u6240\u6709\u6587\u4ef6\u7684\u6587\u4ef6\u540d\u3002\u53ef\u4ee5\u4f7f\u7528pathlib\u6a21\u5757\u7684rglob()\u65b9\u6cd5\u6765\u5b9e\u73b0\u9012\u5f52\u904d\u5386\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<p>directory = Path(&quot;\/home\/user\/documents&quot;)<\/p>\n<p>new_extension = &quot;.bak&quot;<\/p>\n<h2><strong>\u9012\u5f52\u5904\u7406\u76ee\u5f55\u53ca\u5176\u5b50\u76ee\u5f55\u4e2d\u7684\u6240\u6709\u6587\u4ef6<\/strong><\/h2>\n<p>for filepath in directory.rglob(&#39;*&#39;):<\/p>\n<p>    if filepath.is_file():<\/p>\n<p>        new_filepath = filepath.with_suffix(filepath.suffix + new_extension)<\/p>\n<p>        print(new_filepath)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9012\u5f52\u904d\u5386\u6307\u5b9a\u76ee\u5f55\u53ca\u5176\u5b50\u76ee\u5f55\u4e2d\u7684\u6240\u6709\u6587\u4ef6\uff0c\u5e76\u6269\u5c55\u5b83\u4eec\u7684\u6587\u4ef6\u540d\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u9012\u5f52\u5904\u7406\u76ee\u5f55\u7ed3\u6784\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u6269\u5c55\u6587\u4ef6\u540d\u7684\u6700\u4f73\u5b9e\u8df5<\/h3>\n<\/p>\n<p><p>\u5728\u6269\u5c55\u6587\u4ef6\u540d\u65f6\uff0c\u9700\u8981\u6ce8\u610f\u4e00\u4e9b\u6700\u4f73\u5b9e\u8df5\uff0c\u4ee5\u786e\u4fdd\u4ee3\u7801\u7684\u53ef\u9760\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u4f7f\u7528pathlib\u6a21\u5757<\/strong>\uff1apathlib\u6a21\u5757\u63d0\u4f9b\u4e86\u66f4\u73b0\u4ee3\u548c\u76f4\u89c2\u7684\u63a5\u53e3\uff0c\u63a8\u8350\u4f7f\u7528\u8fd9\u4e2a\u6a21\u5757\u6765\u64cd\u4f5c\u6587\u4ef6\u548c\u8def\u5f84\u3002<\/li>\n<li><strong>\u5904\u7406\u7279\u6b8a\u5b57\u7b26<\/strong>\uff1a\u786e\u4fdd\u6587\u4ef6\u540d\u4e2d\u4e0d\u5305\u542b\u7279\u6b8a\u5b57\u7b26\u6216\u4fdd\u7559\u5b57\u7b26\uff0c\u907f\u514d\u6587\u4ef6\u540d\u6269\u5c55\u5931\u8d25\u3002<\/li>\n<li><strong>\u68c0\u67e5\u6587\u4ef6\u5b58\u5728\u6027<\/strong>\uff1a\u5728\u6269\u5c55\u6587\u4ef6\u540d\u4e4b\u524d\uff0c\u68c0\u67e5\u6587\u4ef6\u662f\u5426\u5b58\u5728\uff0c\u907f\u514d\u56e0\u6587\u4ef6\u4e0d\u5b58\u5728\u800c\u5bfc\u81f4\u7684\u9519\u8bef\u3002<\/li>\n<li><strong>\u5904\u7406\u6587\u4ef6\u540d\u51b2\u7a81<\/strong>\uff1a\u5728\u6269\u5c55\u6587\u4ef6\u540d\u65f6\uff0c\u68c0\u67e5\u662f\u5426\u5b58\u5728\u540c\u540d\u6587\u4ef6\uff0c\u907f\u514d\u6587\u4ef6\u540d\u51b2\u7a81\u3002<\/li>\n<li><strong>\u4f7f\u7528\u5f02\u5e38\u5904\u7406<\/strong>\uff1a\u5728\u6587\u4ef6\u64cd\u4f5c\u4e2d\u4f7f\u7528\u5f02\u5e38\u5904\u7406\uff0c\u6355\u83b7\u53ef\u80fd\u7684\u9519\u8bef\u5e76\u8fdb\u884c\u9002\u5f53\u5904\u7406\u3002<\/li>\n<\/ol>\n<p><h3>\u4e5d\u3001\u7efc\u5408\u793a\u4f8b<\/h3>\n<\/p>\n<p><p>\u7ed3\u5408\u4ee5\u4e0a\u6700\u4f73\u5b9e\u8df5\uff0c\u7f16\u5199\u4e00\u4e2a\u7efc\u5408\u793a\u4f8b\u6765\u5904\u7406\u6587\u4ef6\u540d\u6269\u5c55\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<p>def extend_filename(filepath, new_extension):<\/p>\n<p>    try:<\/p>\n<p>        # \u786e\u4fdd\u6587\u4ef6\u5b58\u5728<\/p>\n<p>        if not filepath.exists():<\/p>\n<p>            print(f&quot;File not found: {filepath}&quot;)<\/p>\n<p>            return None<\/p>\n<p>        # \u6269\u5c55\u6587\u4ef6\u540d<\/p>\n<p>        new_filepath = filepath.with_suffix(filepath.suffix + new_extension)<\/p>\n<p>        # \u68c0\u67e5\u662f\u5426\u5b58\u5728\u540c\u540d\u6587\u4ef6<\/p>\n<p>        if new_filepath.exists():<\/p>\n<p>            print(f&quot;File already exists: {new_filepath}&quot;)<\/p>\n<p>            return None<\/p>\n<p>        return new_filepath<\/p>\n<p>    except Exception as e:<\/p>\n<p>        print(f&quot;Error extending filename: {e}&quot;)<\/p>\n<p>        return None<\/p>\n<p>directory = Path(&quot;\/home\/user\/documents&quot;)<\/p>\n<p>new_extension = &quot;.bak&quot;<\/p>\n<p>for filepath in directory.rglob(&#39;*&#39;):<\/p>\n<p>    if filepath.is_file():<\/p>\n<p>        new_filepath = extend_filename(filepath, new_extension)<\/p>\n<p>        if new_filepath:<\/p>\n<p>            print(new_filepath)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u7efc\u5408\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u7f16\u5199\u4e86\u4e00\u4e2aextend_filename()\u51fd\u6570\uff0c\u7528\u4e8e\u6269\u5c55\u6587\u4ef6\u540d\u3002\u51fd\u6570\u4e2d\u5305\u542b\u6587\u4ef6\u5b58\u5728\u6027\u68c0\u67e5\u3001\u6587\u4ef6\u540d\u51b2\u7a81\u68c0\u67e5\u548c\u5f02\u5e38\u5904\u7406\u3002\u7136\u540e\uff0c\u6211\u4eec\u9012\u5f52\u904d\u5386\u6307\u5b9a\u76ee\u5f55\u53ca\u5176\u5b50\u76ee\u5f55\u4e2d\u7684\u6240\u6709\u6587\u4ef6\uff0c\u8c03\u7528extend_filename()\u51fd\u6570\u6765\u6269\u5c55\u6587\u4ef6\u540d\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u6269\u5c55\u6587\u4ef6\u540d\u5728Python\u4e2d\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u6765\u5b9e\u73b0\uff0c\u5305\u62ec\u5b57\u7b26\u4e32\u64cd\u4f5c\u3001os\u6a21\u5757\u548cpathlib\u6a21\u5757\u7b49\u3002\u4f7f\u7528pathlib\u6a21\u5757\u662f\u63a8\u8350\u7684\u65b9\u5f0f\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u66f4\u73b0\u4ee3\u548c\u76f4\u89c2\u7684\u63a5\u53e3\uff0c\u6613\u4e8e\u9605\u8bfb\u548c\u7ef4\u62a4\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u9700\u8981\u6ce8\u610f\u5904\u7406\u590d\u6742\u6587\u4ef6\u8def\u5f84\u3001\u6279\u91cf\u5904\u7406\u6587\u4ef6\u3001\u5904\u7406\u4e0d\u540c\u7c7b\u578b\u7684\u6587\u4ef6\u3001\u9012\u5f52\u76ee\u5f55\u4ee5\u53ca\u9075\u5faa\u6700\u4f73\u5b9e\u8df5\u3002\u901a\u8fc7\u7efc\u5408\u5e94\u7528\u8fd9\u4e9b\u65b9\u6cd5\u548c\u6280\u5de7\uff0c\u53ef\u4ee5\u7f16\u5199\u51fa\u53ef\u9760\u548c\u53ef\u7ef4\u62a4\u7684\u6587\u4ef6\u540d\u6269\u5c55\u4ee3\u7801\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u66f4\u6539\u6587\u4ef6\u6269\u5c55\u540d\uff1f<\/strong><br \/>\u8981\u66f4\u6539\u6587\u4ef6\u7684\u6269\u5c55\u540d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>os<\/code>\u6a21\u5757\u4e2d\u7684<code>rename<\/code>\u51fd\u6570\u3002\u9996\u5148\uff0c\u83b7\u53d6\u6587\u4ef6\u7684\u5f53\u524d\u540d\u79f0\u548c\u6269\u5c55\u540d\uff0c\u7136\u540e\u901a\u8fc7\u5b57\u7b26\u4e32\u64cd\u4f5c\u6765\u521b\u5efa\u65b0\u7684\u6587\u4ef6\u540d\uff0c\u6700\u540e\u5c06\u6587\u4ef6\u91cd\u547d\u540d\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u4ee3\u7801\uff1a  <\/p>\n<pre><code class=\"language-python\">import os\n\ndef change_file_extension(file_path, new_extension):\n    base = os.path.splitext(file_path)[0]\n    new_file_name = f&quot;{base}.{new_extension}&quot;\n    os.rename(file_path, new_file_name)\n    return new_file_name\n<\/code><\/pre>\n<p>\u6b64\u51fd\u6570\u5c06\u6307\u5b9a\u7684\u6587\u4ef6\u8def\u5f84\u7684\u6269\u5c55\u540d\u66f4\u6539\u4e3a\u65b0\u7684\u6269\u5c55\u540d\u3002<\/p>\n<p><strong>\u5982\u4f55\u83b7\u53d6\u6587\u4ef6\u7684\u5f53\u524d\u6269\u5c55\u540d\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>os.path<\/code>\u6a21\u5757\u7684<code>splitext<\/code>\u51fd\u6570\u6765\u83b7\u53d6\u6587\u4ef6\u7684\u6269\u5c55\u540d\u3002\u8be5\u51fd\u6570\u5c06\u8fd4\u56de\u4e00\u4e2a\u5143\u7ec4\uff0c\u5176\u4e2d\u5305\u542b\u6587\u4ef6\u540d\u548c\u6269\u5c55\u540d\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import os\n\ndef get_file_extension(file_path):\n    return os.path.splitext(file_path)[1]\n\n# \u4f7f\u7528\u793a\u4f8b\nfile_extension = get_file_extension(&quot;example.txt&quot;)\nprint(file_extension)  # \u8f93\u51fa: .txt\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u7b80\u5355\u4e14\u6709\u6548\uff0c\u9002\u7528\u4e8e\u4efb\u4f55\u7c7b\u578b\u7684\u6587\u4ef6\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5904\u7406\u591a\u4e2a\u6587\u4ef6\u7684\u6269\u5c55\u540d\uff1f<\/strong><br \/>\u5982\u679c\u9700\u8981\u6279\u91cf\u5904\u7406\u591a\u4e2a\u6587\u4ef6\u7684\u6269\u5c55\u540d\uff0c\u53ef\u4ee5\u7ed3\u5408<code>os<\/code>\u6a21\u5757\u7684<code>listdir<\/code>\u51fd\u6570\u548c\u5faa\u73af\u6765\u5b9e\u73b0\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u5c06\u7279\u5b9a\u76ee\u5f55\u4e0b\u6240\u6709\u6587\u4ef6\u7684\u6269\u5c55\u540d\u66f4\u6539\u4e3a\u65b0\u7684\u6269\u5c55\u540d\uff1a  <\/p>\n<pre><code class=\"language-python\">import os\n\ndef change_extensions_in_directory(directory, new_extension):\n    for filename in os.listdir(directory):\n        if os.path.isfile(os.path.join(directory, filename)):\n            base = os.path.splitext(filename)[0]\n            new_file_name = f&quot;{base}.{new_extension}&quot;\n            os.rename(os.path.join(directory, filename), os.path.join(directory, new_file_name))\n\n# \u4f7f\u7528\u793a\u4f8b\nchange_extensions_in_directory(&quot;your_directory_path&quot;, &quot;jpg&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u4e2a\u793a\u4f8b\u5c06\u6307\u5b9a\u76ee\u5f55\u4e0b\u7684\u6240\u6709\u6587\u4ef6\u7684\u6269\u5c55\u540d\u66f4\u6539\u4e3a<code>.jpg<\/code>\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u6269\u5c55\u6587\u4ef6\u540d\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u6765\u5b9e\u73b0\uff0c\u5982\u4f7f\u7528\u5b57\u7b26\u4e32\u64cd\u4f5c\u3001os\u6a21\u5757\u3001pathlib\u6a21\u5757\u7b49\u3002\u4ee5\u4e0b\u662f\u4e00 [&hellip;]","protected":false},"author":3,"featured_media":1184751,"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\/1184740"}],"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=1184740"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1184740\/revisions"}],"predecessor-version":[{"id":1184752,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1184740\/revisions\/1184752"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1184751"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1184740"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1184740"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1184740"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}