{"id":1168116,"date":"2025-01-15T15:54:05","date_gmt":"2025-01-15T07:54:05","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1168116.html"},"modified":"2025-01-15T15:54:07","modified_gmt":"2025-01-15T07:54:07","slug":"%e5%a6%82%e4%bd%95%e8%8e%b7%e5%8f%96python%e6%89%a9%e5%b1%95%e5%90%8d","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1168116.html","title":{"rendered":"\u5982\u4f55\u83b7\u53d6python\u6269\u5c55\u540d"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26065443\/737e68bd-e796-4ffe-b781-125a247b75e6.webp\" alt=\"\u5982\u4f55\u83b7\u53d6python\u6269\u5c55\u540d\" \/><\/p>\n<p><p> \u8981\u83b7\u53d6Python\u6587\u4ef6\u7684\u6269\u5c55\u540d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u6807\u51c6\u5e93\u4e2d\u7684<code>os<\/code>\u6a21\u5757\u548c<code>os.path<\/code>\u6a21\u5757\u6765\u5b9e\u73b0\u3002<strong>\u901a\u8fc7\u4f7f\u7528<code>os.path.splitext()<\/code>\u51fd\u6570\u3001<code>os.path.basename()<\/code>\u51fd\u6570\u3001\u4ee5\u53ca<code>pathlib<\/code>\u6a21\u5757\u90fd\u53ef\u4ee5\u83b7\u53d6Python\u6587\u4ef6\u7684\u6269\u5c55\u540d<\/strong>\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u51e0\u79cd\u65b9\u6cd5\u4e2d\u7684\u4e00\u79cd\uff1a<strong>\u4f7f\u7528<code>os.path.splitext()<\/code>\u51fd\u6570<\/strong>\u3002<\/p>\n<\/p>\n<p><p><code>os.path.splitext()<\/code>\u51fd\u6570\u53ef\u4ee5\u5c06\u6587\u4ef6\u8def\u5f84\u5206\u5272\u6210\u6587\u4ef6\u540d\u548c\u6269\u5c55\u540d\u4e24\u90e8\u5206\uff0c\u9002\u7528\u4e8e\u5404\u79cd\u64cd\u4f5c\u7cfb\u7edf\u3002\u6211\u4eec\u53ea\u9700\u8981\u4f20\u5165\u6587\u4ef6\u8def\u5f84\uff0c\u7136\u540e\u83b7\u53d6\u8fd4\u56de\u503c\u4e2d\u7684\u7b2c\u4e8c\u90e8\u5206\uff0c\u5373\u53ef\u5f97\u5230\u6587\u4ef6\u7684\u6269\u5c55\u540d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u83b7\u53d6\u6587\u4ef6\u6269\u5c55\u540d\u7684\u51fd\u6570<\/strong><\/h2>\n<p>def get_file_extension(file_path):<\/p>\n<p>    _, extension = os.path.splitext(file_path)<\/p>\n<p>    return extension<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>file_path = &quot;example.py&quot;<\/p>\n<p>print(get_file_extension(file_path))  # \u8f93\u51fa: .py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528<code>os.path.splitext()<\/code>\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528<code>os.path.splitext()<\/code>\u51fd\u6570\u662f\u83b7\u53d6\u6587\u4ef6\u6269\u5c55\u540d\u7684\u4e00\u79cd\u5e38\u89c1\u4e14\u7b80\u5355\u7684\u65b9\u6cd5\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u4ecb\u7ecd\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001\u4ec0\u4e48\u662f<code>os.path.splitext()<\/code>\u51fd\u6570<\/h4>\n<\/p>\n<p><p><code>os.path.splitext()<\/code>\u51fd\u6570\u662fPython\u6807\u51c6\u5e93\u4e2d\u7684\u4e00\u4e2a\u51fd\u6570\uff0c\u5c5e\u4e8e<code>os.path<\/code>\u6a21\u5757\u3002\u8fd9\u4e2a\u51fd\u6570\u7684\u4f5c\u7528\u662f\u5c06\u6587\u4ef6\u8def\u5f84\u62c6\u5206\u6210\u4e24\u4e2a\u90e8\u5206\uff1a\u6587\u4ef6\u540d\u548c\u6269\u5c55\u540d\u3002\u8fd4\u56de\u503c\u662f\u4e00\u4e2a\u5305\u542b\u4e24\u4e2a\u5143\u7d20\u7684\u5143\u7ec4\uff0c\u7b2c\u4e00\u4e2a\u5143\u7d20\u662f\u6587\u4ef6\u540d\uff0c\u7b2c\u4e8c\u4e2a\u5143\u7d20\u662f\u6269\u5c55\u540d\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u5982\u4f55\u4f7f\u7528<code>os.path.splitext()<\/code>\u51fd\u6570<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528\u8be5\u51fd\u6570\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u8981\u4f20\u5165\u4e00\u4e2a\u6587\u4ef6\u8def\u5f84\u4f5c\u4e3a\u53c2\u6570\u5373\u53ef\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>def get_file_extension(file_path):<\/p>\n<p>    # \u4f7f\u7528os.path.splitext()\u51fd\u6570\u62c6\u5206\u6587\u4ef6\u8def\u5f84<\/p>\n<p>    file_name, extension = os.path.splitext(file_path)<\/p>\n<p>    return extension<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>file_path = &quot;example.py&quot;<\/p>\n<p>print(get_file_extension(file_path))  # \u8f93\u51fa: .py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u51fd\u6570<code>get_file_extension()<\/code>\uff0c\u8be5\u51fd\u6570\u63a5\u6536\u4e00\u4e2a\u6587\u4ef6\u8def\u5f84\u4f5c\u4e3a\u53c2\u6570\uff0c\u5e76\u4f7f\u7528<code>os.path.splitext()<\/code>\u51fd\u6570\u62c6\u5206\u6587\u4ef6\u8def\u5f84\u3002\u6700\u540e\u8fd4\u56de\u6269\u5c55\u540d\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u5e94\u7528\u573a\u666f<\/h4>\n<\/p>\n<p><p><code>os.path.splitext()<\/code>\u51fd\u6570\u9002\u7528\u4e8e\u9700\u8981\u4ece\u6587\u4ef6\u8def\u5f84\u4e2d\u63d0\u53d6\u6269\u5c55\u540d\u7684\u5404\u79cd\u573a\u666f\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u68c0\u67e5\u6587\u4ef6\u7c7b\u578b\u3002<\/li>\n<li>\u6839\u636e\u6587\u4ef6\u6269\u5c55\u540d\u8fdb\u884c\u4e0d\u540c\u7684\u5904\u7406\u3002<\/li>\n<li>\u6279\u91cf\u5904\u7406\u6587\u4ef6\u65f6\uff0c\u5bf9\u4e0d\u540c\u7c7b\u578b\u7684\u6587\u4ef6\u8fdb\u884c\u5206\u7c7b\u3002<\/li>\n<\/ul>\n<p><h3>\u4e8c\u3001\u4f7f\u7528<code>os.path.basename()<\/code>\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u53e6\u4e00\u79cd\u83b7\u53d6\u6587\u4ef6\u6269\u5c55\u540d\u7684\u65b9\u6cd5\u662f\u4f7f\u7528<code>os.path.basename()<\/code>\u51fd\u6570\u3002\u8be5\u65b9\u6cd5\u7684\u601d\u8def\u662f\u5148\u83b7\u53d6\u6587\u4ef6\u7684\u57fa\u672c\u540d\uff08\u4e0d\u5305\u542b\u76ee\u5f55\u8def\u5f84\uff09\uff0c\u7136\u540e\u518d\u62c6\u5206\u57fa\u672c\u540d\u4ee5\u83b7\u53d6\u6269\u5c55\u540d\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u4ec0\u4e48\u662f<code>os.path.basename()<\/code>\u51fd\u6570<\/h4>\n<\/p>\n<p><p><code>os.path.basename()<\/code>\u51fd\u6570\u540c\u6837\u5c5e\u4e8e<code>os.path<\/code>\u6a21\u5757\u3002\u5b83\u7684\u4f5c\u7528\u662f\u8fd4\u56de\u6587\u4ef6\u8def\u5f84\u4e2d\u7684\u57fa\u672c\u540d\uff08\u5373\u6587\u4ef6\u540d\u90e8\u5206\uff09\uff0c\u4e0d\u5305\u542b\u76ee\u5f55\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u5982\u4f55\u4f7f\u7528<code>os.path.basename()<\/code>\u51fd\u6570<\/h4>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>def get_file_extension(file_path):<\/p>\n<p>    # \u4f7f\u7528os.path.basename()\u51fd\u6570\u83b7\u53d6\u57fa\u672c\u540d<\/p>\n<p>    base_name = os.path.basename(file_path)<\/p>\n<p>    # \u62c6\u5206\u57fa\u672c\u540d\u4ee5\u83b7\u53d6\u6269\u5c55\u540d<\/p>\n<p>    _, extension = os.path.splitext(base_name)<\/p>\n<p>    return extension<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>file_path = &quot;\/path\/to\/example.py&quot;<\/p>\n<p>print(get_file_extension(file_path))  # \u8f93\u51fa: .py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5148\u4f7f\u7528<code>os.path.basename()<\/code>\u51fd\u6570\u83b7\u53d6\u6587\u4ef6\u8def\u5f84\u4e2d\u7684\u57fa\u672c\u540d\uff0c\u7136\u540e\u518d\u4f7f\u7528<code>os.path.splitext()<\/code>\u51fd\u6570\u62c6\u5206\u57fa\u672c\u540d\u4ee5\u83b7\u53d6\u6269\u5c55\u540d\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u5e94\u7528\u573a\u666f<\/h4>\n<\/p>\n<p><p><code>os.path.basename()<\/code>\u51fd\u6570\u9002\u7528\u4e8e\u9700\u8981\u4ece\u5b8c\u6574\u7684\u6587\u4ef6\u8def\u5f84\u4e2d\u63d0\u53d6\u6587\u4ef6\u540d\u548c\u6269\u5c55\u540d\u7684\u573a\u666f\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u663e\u793a\u6587\u4ef6\u540d\u800c\u4e0d\u5305\u542b\u76ee\u5f55\u8def\u5f84\u3002<\/li>\n<li>\u4ece\u6587\u4ef6\u8def\u5f84\u4e2d\u63d0\u53d6\u6587\u4ef6\u540d\u4ee5\u8fdb\u884c\u91cd\u547d\u540d\u6216\u5176\u4ed6\u64cd\u4f5c\u3002<\/li>\n<\/ul>\n<p><h3>\u4e09\u3001\u4f7f\u7528<code>pathlib<\/code>\u6a21\u5757<\/h3>\n<\/p>\n<p><p>Python 3.4\u5f15\u5165\u4e86<code>pathlib<\/code>\u6a21\u5757\uff0c\u8fd9\u4e2a\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u79cd\u9762\u5411\u5bf9\u8c61\u7684\u65b9\u6cd5\u6765\u5904\u7406\u6587\u4ef6\u7cfb\u7edf\u8def\u5f84\u3002\u4f7f\u7528<code>pathlib<\/code>\u6a21\u5757\u4e5f\u53ef\u4ee5\u975e\u5e38\u65b9\u4fbf\u5730\u83b7\u53d6\u6587\u4ef6\u6269\u5c55\u540d\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u4ec0\u4e48\u662f<code>pathlib<\/code>\u6a21\u5757<\/h4>\n<\/p>\n<p><p><code>pathlib<\/code>\u6a21\u5757\u662fPython\u6807\u51c6\u5e93\u7684\u4e00\u90e8\u5206\uff0c\u5b83\u63d0\u4f9b\u4e86<code>Path<\/code>\u7c7b\u6765\u5904\u7406\u6587\u4ef6\u7cfb\u7edf\u8def\u5f84\u3002<code>Path<\/code>\u7c7b\u7684\u65b9\u6cd5\u548c\u5c5e\u6027\u53ef\u4ee5\u7528\u4e8e\u5404\u79cd\u6587\u4ef6\u8def\u5f84\u64cd\u4f5c\uff0c\u5305\u62ec\u83b7\u53d6\u6587\u4ef6\u6269\u5c55\u540d\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u5982\u4f55\u4f7f\u7528<code>pathlib<\/code>\u6a21\u5757<\/h4>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<p>def get_file_extension(file_path):<\/p>\n<p>    # \u4f7f\u7528Path\u7c7b\u83b7\u53d6\u6587\u4ef6\u8def\u5f84\u5bf9\u8c61<\/p>\n<p>    path = Path(file_path)<\/p>\n<p>    # \u83b7\u53d6\u6587\u4ef6\u6269\u5c55\u540d<\/p>\n<p>    return path.suffix<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>file_path = &quot;example.py&quot;<\/p>\n<p>print(get_file_extension(file_path))  # \u8f93\u51fa: .py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>pathlib.Path<\/code>\u7c7b\u521b\u5efa\u4e00\u4e2a\u6587\u4ef6\u8def\u5f84\u5bf9\u8c61\uff0c\u7136\u540e\u4f7f\u7528<code>Path.suffix<\/code>\u5c5e\u6027\u83b7\u53d6\u6587\u4ef6\u7684\u6269\u5c55\u540d\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u5e94\u7528\u573a\u666f<\/h4>\n<\/p>\n<p><p><code>pathlib<\/code>\u6a21\u5757\u9002\u7528\u4e8e\u9700\u8981\u5904\u7406\u6587\u4ef6\u7cfb\u7edf\u8def\u5f84\u7684\u5404\u79cd\u573a\u666f\uff0c\u5c24\u5176\u662f\u9700\u8981\u8fdb\u884c\u590d\u6742\u8def\u5f84\u64cd\u4f5c\u65f6\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u5904\u7406\u8de8\u5e73\u53f0\u6587\u4ef6\u8def\u5f84\u3002<\/li>\n<li>\u8fdb\u884c\u8def\u5f84\u62fc\u63a5\u3001\u62c6\u5206\u548c\u89e3\u6790\u3002<\/li>\n<li>\u6279\u91cf\u5904\u7406\u6587\u4ef6\u548c\u76ee\u5f55\u3002<\/li>\n<\/ul>\n<p><h3>\u56db\u3001\u5b9e\u9645\u5e94\u7528\u4e2d\u7684\u6ce8\u610f\u4e8b\u9879<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u83b7\u53d6\u6587\u4ef6\u6269\u5c55\u540d\u53ef\u80fd\u4f1a\u9047\u5230\u4e00\u4e9b\u7279\u6b8a\u60c5\u51b5\u548c\u6ce8\u610f\u4e8b\u9879\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u95ee\u9898\u548c\u89e3\u51b3\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001\u5904\u7406\u6ca1\u6709\u6269\u5c55\u540d\u7684\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u6709\u4e9b\u6587\u4ef6\u53ef\u80fd\u6ca1\u6709\u6269\u5c55\u540d\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u83b7\u53d6\u5230\u7684\u6269\u5c55\u540d\u5c06\u662f\u4e00\u4e2a\u7a7a\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">file_path = &quot;example&quot;<\/p>\n<p>print(get_file_extension(file_path))  # \u8f93\u51fa: &#39;&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u5904\u7406\u9690\u85cf\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u64cd\u4f5c\u7cfb\u7edf\uff08\u5982Unix\u548cLinux\uff09\u4e2d\uff0c\u9690\u85cf\u6587\u4ef6\u4ee5\u70b9\u53f7\uff08.\uff09\u5f00\u5934\u3002\u8fd9\u4e9b\u6587\u4ef6\u7684\u6269\u5c55\u540d\u53ef\u80fd\u4f1a\u88ab\u8bef\u8ba4\u4e3a\u662f\u6587\u4ef6\u540d\u7684\u4e00\u90e8\u5206\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">file_path = &quot;.hiddenfile&quot;<\/p>\n<p>print(get_file_extension(file_path))  # \u8f93\u51fa: &#39;&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u5904\u7406\u591a\u91cd\u6269\u5c55\u540d<\/h4>\n<\/p>\n<p><p>\u6709\u4e9b\u6587\u4ef6\u53ef\u80fd\u5305\u542b\u591a\u4e2a\u6269\u5c55\u540d\uff0c\u4f8b\u5982\u538b\u7f29\u6587\u4ef6<code>archive.tar.gz<\/code>\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u83b7\u53d6\u5230\u7684\u6269\u5c55\u540d\u5c06\u662f\u6700\u540e\u4e00\u4e2a\u6269\u5c55\u540d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">file_path = &quot;archive.tar.gz&quot;<\/p>\n<p>print(get_file_extension(file_path))  # \u8f93\u51fa: .gz<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e3a\u4e86\u5904\u7406\u591a\u91cd\u6269\u5c55\u540d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u64cd\u4f5c\u6765\u83b7\u53d6\u6240\u6709\u6269\u5c55\u540d\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def get_all_extensions(file_path):<\/p>\n<p>    base_name = os.path.basename(file_path)<\/p>\n<p>    return base_name.split(&#39;.&#39;)[1:]<\/p>\n<p>file_path = &quot;archive.tar.gz&quot;<\/p>\n<p>print(get_all_extensions(file_path))  # \u8f93\u51fa: [&#39;tar&#39;, &#39;gz&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u7efc\u5408\u6bd4\u8f83<\/h3>\n<\/p>\n<p><p>\u5728\u4e0d\u540c\u7684\u5e94\u7528\u573a\u666f\u4e2d\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u6765\u83b7\u53d6\u6587\u4ef6\u6269\u5c55\u540d\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u4ee5\u4e0b\u662f\u5bf9\u4e0a\u8ff0\u51e0\u79cd\u65b9\u6cd5\u7684\u7efc\u5408\u6bd4\u8f83\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001<code>os.path.splitext()<\/code>\u51fd\u6570<\/h4>\n<\/p>\n<p><p>\u4f18\u70b9\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u7b80\u5355\u6613\u7528\u3002<\/li>\n<li>\u9002\u7528\u4e8e\u5404\u79cd\u64cd\u4f5c\u7cfb\u7edf\u3002<\/li>\n<li>\u8fd4\u56de\u503c\u5305\u542b\u6587\u4ef6\u540d\u548c\u6269\u5c55\u540d\u3002<\/li>\n<\/ul>\n<p><p>\u7f3a\u70b9\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u5904\u7406\u591a\u91cd\u6269\u5c55\u540d\u65f6\u9700\u8981\u989d\u5916\u5904\u7406\u3002<\/li>\n<\/ul>\n<p><h4>2\u3001<code>os.path.basename()<\/code>\u51fd\u6570<\/h4>\n<\/p>\n<p><p>\u4f18\u70b9\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u53ef\u4ee5\u4ece\u5b8c\u6574\u8def\u5f84\u4e2d\u63d0\u53d6\u6587\u4ef6\u540d\u3002<\/li>\n<li>\u4e0e<code>os.path.splitext()<\/code>\u7ed3\u5408\u4f7f\u7528\u6548\u679c\u66f4\u597d\u3002<\/li>\n<\/ul>\n<p><p>\u7f3a\u70b9\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u9700\u8981\u4e0e\u5176\u4ed6\u51fd\u6570\u7ed3\u5408\u4f7f\u7528\u624d\u80fd\u83b7\u53d6\u6269\u5c55\u540d\u3002<\/li>\n<\/ul>\n<p><h4>3\u3001<code>pathlib<\/code>\u6a21\u5757<\/h4>\n<\/p>\n<p><p>\u4f18\u70b9\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u9762\u5411\u5bf9\u8c61\u7684\u65b9\u5f0f\uff0c\u66f4\u52a0\u76f4\u89c2\u3002<\/li>\n<li>\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u8def\u5f84\u64cd\u4f5c\u65b9\u6cd5\u548c\u5c5e\u6027\u3002<\/li>\n<li>\u9002\u7528\u4e8e\u590d\u6742\u8def\u5f84\u64cd\u4f5c\u3002<\/li>\n<\/ul>\n<p><p>\u7f3a\u70b9\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u9700\u8981Python 3.4\u53ca\u4ee5\u4e0a\u7248\u672c\u3002<\/li>\n<li>\u5904\u7406\u591a\u91cd\u6269\u5c55\u540d\u65f6\u9700\u8981\u989d\u5916\u5904\u7406\u3002<\/li>\n<\/ul>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u83b7\u53d6Python\u6587\u4ef6\u7684\u6269\u5c55\u540d\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u64cd\u4f5c\uff0c\u5728\u4e0d\u540c\u7684\u5e94\u7528\u573a\u666f\u4e2d\u53ef\u4ee5\u9009\u62e9\u4e0d\u540c\u7684\u65b9\u6cd5\u3002<strong>\u901a\u8fc7\u4f7f\u7528<code>os.path.splitext()<\/code>\u51fd\u6570\u3001<code>os.path.basename()<\/code>\u51fd\u6570\u3001\u4ee5\u53ca<code>pathlib<\/code>\u6a21\u5757\u90fd\u53ef\u4ee5\u83b7\u53d6Python\u6587\u4ef6\u7684\u6269\u5c55\u540d<\/strong>\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u4f18\u7f3a\u70b9\u548c\u9002\u7528\u573a\u666f\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u8fd8\u9700\u8981\u6ce8\u610f\u5904\u7406\u6ca1\u6709\u6269\u5c55\u540d\u7684\u6587\u4ef6\u3001\u9690\u85cf\u6587\u4ef6\u548c\u591a\u91cd\u6269\u5c55\u540d\u7b49\u7279\u6b8a\u60c5\u51b5\u3002\u901a\u8fc7\u5408\u7406\u9009\u62e9\u548c\u7ec4\u5408\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u9ad8\u6548\u5730\u83b7\u53d6\u548c\u5904\u7406\u6587\u4ef6\u6269\u5c55\u540d\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u4ec0\u4e48\u662fPython\u6269\u5c55\u540d\uff0c\u4e3a\u4ec0\u4e48\u91cd\u8981\uff1f<\/strong><br \/>Python\u6269\u5c55\u540d\u901a\u5e38\u662f\u201c.py\u201d\uff0c\u5b83\u4ee3\u8868Python\u811a\u672c\u6587\u4ef6\u3002\u4e86\u89e3\u6269\u5c55\u540d\u7684\u610f\u4e49\u5bf9\u4e8e\u6587\u4ef6\u7ba1\u7406\u548c\u4ee3\u7801\u7ec4\u7ec7\u81f3\u5173\u91cd\u8981\uff0c\u7279\u522b\u662f\u5728\u5f00\u53d1\u548c\u5171\u4eab\u4ee3\u7801\u65f6\u3002\u4f7f\u7528\u6b63\u786e\u7684\u6269\u5c55\u540d\u53ef\u4ee5\u5e2e\u52a9\u64cd\u4f5c\u7cfb\u7edf\u548c\u5f00\u53d1\u73af\u5883\u8bc6\u522b\u6587\u4ef6\u7c7b\u578b\uff0c\u4ece\u800c\u6b63\u786e\u5730\u6267\u884c\u6216\u7f16\u8f91\u4ee3\u7801\u3002<\/p>\n<p><strong>\u5728Windows\u548cMac\u4e0a\u5982\u4f55\u67e5\u627e\u6587\u4ef6\u7684\u6269\u5c55\u540d\uff1f<\/strong><br \/>\u5728Windows\u7cfb\u7edf\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u6587\u4ef6<a href=\"https:\/\/docs.pingcode.com\/blog\/project-management\/58557.html\" target=\"_blank\">\u8d44\u6e90\u7ba1\u7406<\/a>\u5668\u67e5\u770b\u6587\u4ef6\u6269\u5c55\u540d\u3002\u53ea\u9700\u53f3\u952e\u5355\u51fb\u6587\u4ef6\uff0c\u9009\u62e9\u201c\u5c5e\u6027\u201d\uff0c\u5c31\u80fd\u770b\u5230\u6587\u4ef6\u7684\u8be6\u7ec6\u4fe1\u606f\uff0c\u5305\u62ec\u6269\u5c55\u540d\u3002\u5bf9\u4e8eMac\u7528\u6237\uff0c\u53ef\u4ee5\u5728Finder\u4e2d\u9009\u62e9\u6587\u4ef6\uff0c\u6309\u4e0b\u201cCommand + I\u201d\u4ee5\u6253\u5f00\u4fe1\u606f\u7a97\u53e3\uff0c\u67e5\u770b\u6269\u5c55\u540d\u3002\u786e\u4fdd\u5728\u6587\u4ef6\u5939\u89c6\u56fe\u4e2d\u663e\u793a\u6269\u5c55\u540d\u7684\u9009\u9879\u5df2\u5f00\u542f\uff0c\u4ee5\u4fbf\u66f4\u65b9\u4fbf\u5730\u7ba1\u7406\u6587\u4ef6\u3002<\/p>\n<p><strong>\u5982\u4f55\u901a\u8fc7Python\u4ee3\u7801\u83b7\u53d6\u6587\u4ef6\u7684\u6269\u5c55\u540d\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528Python\u5185\u7f6e\u7684<code>os<\/code>\u6a21\u5757\u6216<code>pathlib<\/code>\u6a21\u5757\u6765\u83b7\u53d6\u6587\u4ef6\u7684\u6269\u5c55\u540d\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>os.path.splitext()<\/code>\u51fd\u6570\u53ef\u4ee5\u5c06\u6587\u4ef6\u540d\u5206\u5272\u6210\u6587\u4ef6\u540d\u548c\u6269\u5c55\u540d\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">import os\n\nfilename = &quot;example.py&quot;\nextension = os.path.splitext(filename)[1]\nprint(extension)  # \u8f93\u51fa: .py\n<\/code><\/pre>\n<p>\u6b64\u5916\uff0c<code>pathlib<\/code>\u6a21\u5757\u4e5f\u53ef\u4ee5\u5b9e\u73b0\u76f8\u4f3c\u7684\u529f\u80fd\uff0c\u4f7f\u7528\u66f4\u73b0\u4ee3\u7684\u65b9\u5f0f\u5904\u7406\u8def\u5f84\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u83b7\u53d6Python\u6587\u4ef6\u7684\u6269\u5c55\u540d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u6807\u51c6\u5e93\u4e2d\u7684os\u6a21\u5757\u548cos.path\u6a21\u5757\u6765\u5b9e\u73b0\u3002\u901a\u8fc7\u4f7f\u7528os.pat [&hellip;]","protected":false},"author":3,"featured_media":1168119,"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\/1168116"}],"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=1168116"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1168116\/revisions"}],"predecessor-version":[{"id":1168120,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1168116\/revisions\/1168120"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1168119"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1168116"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1168116"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1168116"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}