{"id":1109802,"date":"2025-01-08T17:13:41","date_gmt":"2025-01-08T09:13:41","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1109802.html"},"modified":"2025-01-08T17:13:44","modified_gmt":"2025-01-08T09:13:44","slug":"python%e5%a6%82%e4%bd%95%e8%8e%b7%e5%8f%96%e6%96%87%e4%bb%b6%e5%90%8d%e5%ad%97%e7%ac%a6%e4%b8%b2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1109802.html","title":{"rendered":"python\u5982\u4f55\u83b7\u53d6\u6587\u4ef6\u540d\u5b57\u7b26\u4e32"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25072919\/cf5549e8-afae-41d0-8775-6950e21e8c1f.webp\" alt=\"python\u5982\u4f55\u83b7\u53d6\u6587\u4ef6\u540d\u5b57\u7b26\u4e32\" \/><\/p>\n<p><p> <strong>Python\u83b7\u53d6\u6587\u4ef6\u540d\u5b57\u7b26\u4e32\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u4e3b\u8981\u5305\u62ec\u4f7f\u7528os\u6a21\u5757\u3001Pathlib\u6a21\u5757\u3001glob\u6a21\u5757\u7b49\u3002<\/strong>\u5176\u4e2d\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u4f7f\u7528os\u6a21\u5757\u4e2d\u7684os.path.basename()\u51fd\u6570\u548cPathlib\u6a21\u5757\u4e2d\u7684Path().name\u5c5e\u6027\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\u7684\u4f7f\u7528\u65b9\u5f0f\uff0c\u5e76\u7ed9\u51fa\u4ee3\u7801\u793a\u4f8b\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528os\u6a21\u5757<\/p>\n<\/p>\n<p><p>os\u6a21\u5757\u662fPython\u6807\u51c6\u5e93\u4e2d\u7684\u4e00\u4e2a\u6a21\u5757\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e0e\u64cd\u4f5c\u7cfb\u7edf\u8fdb\u884c\u4ea4\u4e92\u7684\u529f\u80fd\u3002os.path\u6a21\u5757\u63d0\u4f9b\u4e86\u8bb8\u591a\u7528\u4e8e\u5904\u7406\u6587\u4ef6\u8def\u5f84\u7684\u65b9\u6cd5\uff0c\u5176\u4e2d\u5305\u62ec\u83b7\u53d6\u6587\u4ef6\u540d\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u83b7\u53d6\u6587\u4ef6\u540d<\/strong><\/h2>\n<p>file_path = &#39;\/path\/to\/your\/file.txt&#39;<\/p>\n<p>file_name = os.path.basename(file_path)<\/p>\n<p>print(f&quot;\u6587\u4ef6\u540d\u662f: {file_name}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5bfc\u5165\u4e86os\u6a21\u5757\uff0c\u7136\u540e\u5b9a\u4e49\u4e86\u4e00\u4e2a\u6587\u4ef6\u8def\u5f84<code>file_path<\/code>\u3002\u63a5\u7740\uff0c\u6211\u4eec\u4f7f\u7528<code>os.path.basename()<\/code>\u51fd\u6570\u83b7\u53d6\u6587\u4ef6\u540d\uff0c\u5e76\u5c06\u7ed3\u679c\u5b58\u50a8\u5728<code>file_name<\/code>\u53d8\u91cf\u4e2d\uff0c\u6700\u540e\u6253\u5370\u6587\u4ef6\u540d\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528os\u6a21\u5757\u7684\u4f18\u70b9\u662f\u517c\u5bb9\u6027\u597d\u3001\u4f7f\u7528\u7b80\u5355\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528Pathlib\u6a21\u5757<\/p>\n<\/p>\n<p><p>Pathlib\u6a21\u5757\u662fPython 3.4\u5f15\u5165\u7684\u4e00\u4e2a\u6a21\u5757\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e00\u79cd\u9762\u5411\u5bf9\u8c61\u7684\u65b9\u5f0f\u6765\u5904\u7406\u6587\u4ef6\u548c\u76ee\u5f55\u8def\u5f84\u3002Pathlib\u6a21\u5757\u4e2d\u7684Path\u7c7b\u53ef\u4ee5\u65b9\u4fbf\u5730\u83b7\u53d6\u6587\u4ef6\u540d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<h2><strong>\u83b7\u53d6\u6587\u4ef6\u540d<\/strong><\/h2>\n<p>file_path = Path(&#39;\/path\/to\/your\/file.txt&#39;)<\/p>\n<p>file_name = file_path.name<\/p>\n<p>print(f&quot;\u6587\u4ef6\u540d\u662f: {file_name}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u4ecepathlib\u6a21\u5757\u4e2d\u5bfc\u5165\u4e86Path\u7c7b\uff0c\u7136\u540e\u5b9a\u4e49\u4e86\u4e00\u4e2a\u6587\u4ef6\u8def\u5f84<code>file_path<\/code>\uff0c\u5c06\u5176\u8f6c\u6362\u4e3aPath\u5bf9\u8c61\u3002\u63a5\u7740\uff0c\u6211\u4eec\u4f7f\u7528Path\u5bf9\u8c61\u7684<code>name<\/code>\u5c5e\u6027\u83b7\u53d6\u6587\u4ef6\u540d\uff0c\u5e76\u5c06\u7ed3\u679c\u5b58\u50a8\u5728<code>file_name<\/code>\u53d8\u91cf\u4e2d\uff0c\u6700\u540e\u6253\u5370\u6587\u4ef6\u540d\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528Pathlib\u6a21\u5757\u7684\u4f18\u70b9\u662f\u4ee3\u7801\u7b80\u6d01\u3001\u9762\u5411\u5bf9\u8c61\uff0c\u9002\u5408\u5904\u7406\u590d\u6742\u7684\u8def\u5f84\u64cd\u4f5c\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528glob\u6a21\u5757<\/p>\n<\/p>\n<p><p>glob\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u4e2a\u6587\u4ef6\u540d\u6a21\u5f0f\u5339\u914d\u5de5\u5177\uff0c\u53ef\u4ee5\u7528\u6765\u83b7\u53d6\u7b26\u5408\u7279\u5b9a\u6a21\u5f0f\u7684\u6587\u4ef6\u540d\u3002\u867d\u7136glob\u6a21\u5757\u4e3b\u8981\u7528\u4e8e\u67e5\u627e\u5339\u914d\u7279\u5b9a\u6a21\u5f0f\u7684\u6587\u4ef6\uff0c\u4f46\u4e5f\u53ef\u4ee5\u7528\u6765\u83b7\u53d6\u6587\u4ef6\u540d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import glob<\/p>\n<h2><strong>\u83b7\u53d6\u6587\u4ef6\u540d<\/strong><\/h2>\n<p>file_path = &#39;\/path\/to\/your\/file.txt&#39;<\/p>\n<p>file_name = glob.glob(file_path)[0].split(&#39;\/&#39;)[-1]<\/p>\n<p>print(f&quot;\u6587\u4ef6\u540d\u662f: {file_name}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5bfc\u5165\u4e86glob\u6a21\u5757\uff0c\u7136\u540e\u5b9a\u4e49\u4e86\u4e00\u4e2a\u6587\u4ef6\u8def\u5f84<code>file_path<\/code>\u3002\u63a5\u7740\uff0c\u6211\u4eec\u4f7f\u7528<code>glob.glob()<\/code>\u51fd\u6570\u83b7\u53d6\u5339\u914d\u7684\u6587\u4ef6\u8def\u5f84\u5217\u8868\uff0c\u5e76\u901a\u8fc7\u5217\u8868\u7d22\u5f15\u83b7\u53d6\u7b2c\u4e00\u4e2a\u5339\u914d\u7684\u6587\u4ef6\u8def\u5f84\u3002\u6700\u540e\uff0c\u6211\u4eec\u4f7f\u7528\u5b57\u7b26\u4e32\u7684<code>split()<\/code>\u65b9\u6cd5\u5206\u5272\u8def\u5f84\uff0c\u5e76\u83b7\u53d6\u6700\u540e\u4e00\u4e2a\u90e8\u5206\u4f5c\u4e3a\u6587\u4ef6\u540d\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528glob\u6a21\u5757\u7684\u4f18\u70b9\u662f\u53ef\u4ee5\u8fdb\u884c\u6a21\u5f0f\u5339\u914d\uff0c\u9002\u5408\u6279\u91cf\u5904\u7406\u6587\u4ef6\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u56db\u3001\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f<\/p>\n<\/p>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u6587\u672c\u5904\u7406\u5de5\u5177\uff0c\u53ef\u4ee5\u7528\u6765\u5339\u914d\u548c\u63d0\u53d6\u6587\u4ef6\u540d\u3002Python\u7684re\u6a21\u5757\u63d0\u4f9b\u4e86\u5bf9\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u652f\u6301\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<h2><strong>\u83b7\u53d6\u6587\u4ef6\u540d<\/strong><\/h2>\n<p>file_path = &#39;\/path\/to\/your\/file.txt&#39;<\/p>\n<p>file_name = re.search(r&#39;[^\/\\\\]+$&#39;, file_path).group()<\/p>\n<p>print(f&quot;\u6587\u4ef6\u540d\u662f: {file_name}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5bfc\u5165\u4e86re\u6a21\u5757\uff0c\u7136\u540e\u5b9a\u4e49\u4e86\u4e00\u4e2a\u6587\u4ef6\u8def\u5f84<code>file_path<\/code>\u3002\u63a5\u7740\uff0c\u6211\u4eec\u4f7f\u7528<code>re.search()<\/code>\u51fd\u6570\u67e5\u627e\u6587\u4ef6\u8def\u5f84\u4e2d\u6700\u540e\u4e00\u4e2a\u659c\u6760\u540e\u7684\u90e8\u5206\uff0c\u5e76\u5c06\u7ed3\u679c\u5b58\u50a8\u5728<code>file_name<\/code>\u53d8\u91cf\u4e2d\uff0c\u6700\u540e\u6253\u5370\u6587\u4ef6\u540d\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u4f18\u70b9\u662f\u7075\u6d3b\u3001\u5f3a\u5927\uff0c\u53ef\u4ee5\u5904\u7406\u590d\u6742\u7684\u6587\u4ef6\u8def\u5f84\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4e94\u3001\u83b7\u53d6\u6587\u4ef6\u540d\u548c\u6269\u5c55\u540d<\/p>\n<\/p>\n<p><p>\u6709\u65f6\u6211\u4eec\u4e0d\u4ec5\u9700\u8981\u83b7\u53d6\u6587\u4ef6\u540d\uff0c\u8fd8\u9700\u8981\u83b7\u53d6\u6587\u4ef6\u7684\u6269\u5c55\u540d\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528os.path.splitext()\u51fd\u6570\u6216Pathlib\u6a21\u5757\u7684suffix\u5c5e\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u83b7\u53d6\u6587\u4ef6\u540d\u548c\u6269\u5c55\u540d<\/strong><\/h2>\n<p>file_path = &#39;\/path\/to\/your\/file.txt&#39;<\/p>\n<p>file_name, file_extension = os.path.splitext(os.path.basename(file_path))<\/p>\n<p>print(f&quot;\u6587\u4ef6\u540d\u662f: {file_name}, \u6269\u5c55\u540d\u662f: {file_extension}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u4f7f\u7528<code>os.path.basename()<\/code>\u51fd\u6570\u83b7\u53d6\u6587\u4ef6\u540d\uff0c\u7136\u540e\u4f7f\u7528<code>os.path.splitext()<\/code>\u51fd\u6570\u5c06\u6587\u4ef6\u540d\u5206\u5272\u4e3a\u6587\u4ef6\u540d\u548c\u6269\u5c55\u540d\uff0c\u5e76\u5c06\u7ed3\u679c\u5206\u522b\u5b58\u50a8\u5728<code>file_name<\/code>\u548c<code>file_extension<\/code>\u53d8\u91cf\u4e2d\uff0c\u6700\u540e\u6253\u5370\u6587\u4ef6\u540d\u548c\u6269\u5c55\u540d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<h2><strong>\u83b7\u53d6\u6587\u4ef6\u540d\u548c\u6269\u5c55\u540d<\/strong><\/h2>\n<p>file_path = Path(&#39;\/path\/to\/your\/file.txt&#39;)<\/p>\n<p>file_name = file_path.stem<\/p>\n<p>file_extension = file_path.suffix<\/p>\n<p>print(f&quot;\u6587\u4ef6\u540d\u662f: {file_name}, \u6269\u5c55\u540d\u662f: {file_extension}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528Pathlib\u6a21\u5757\u7684Path\u7c7b\u7684<code>stem<\/code>\u5c5e\u6027\u83b7\u53d6\u6587\u4ef6\u540d\uff0c\u4f7f\u7528<code>suffix<\/code>\u5c5e\u6027\u83b7\u53d6\u6269\u5c55\u540d\uff0c\u5e76\u5c06\u7ed3\u679c\u5206\u522b\u5b58\u50a8\u5728<code>file_name<\/code>\u548c<code>file_extension<\/code>\u53d8\u91cf\u4e2d\uff0c\u6700\u540e\u6253\u5370\u6587\u4ef6\u540d\u548c\u6269\u5c55\u540d\u3002<\/p>\n<\/p>\n<p><p><strong>\u83b7\u53d6\u6587\u4ef6\u540d\u548c\u6269\u5c55\u540d\u7684\u65b9\u6cd5\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5728\u5904\u7406\u6587\u4ef6\u65f6\u66f4\u7075\u6d3b\u5730\u8fdb\u884c\u64cd\u4f5c\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u516d\u3001\u5904\u7406\u5e26\u6709\u8def\u5f84\u7684\u6587\u4ef6\u540d<\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u5904\u7406\u5e26\u6709\u8def\u5f84\u7684\u6587\u4ef6\u540d\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528os.path.dirname()\u51fd\u6570\u6216Pathlib\u6a21\u5757\u7684parent\u5c5e\u6027\u83b7\u53d6\u6587\u4ef6\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u83b7\u53d6\u6587\u4ef6\u8def\u5f84\u548c\u6587\u4ef6\u540d<\/strong><\/h2>\n<p>file_path = &#39;\/path\/to\/your\/file.txt&#39;<\/p>\n<p>file_directory = os.path.dirname(file_path)<\/p>\n<p>file_name = os.path.basename(file_path)<\/p>\n<p>print(f&quot;\u6587\u4ef6\u8def\u5f84\u662f: {file_directory}, \u6587\u4ef6\u540d\u662f: {file_name}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u4f7f\u7528<code>os.path.dirname()<\/code>\u51fd\u6570\u83b7\u53d6\u6587\u4ef6\u8def\u5f84\uff0c\u7136\u540e\u4f7f\u7528<code>os.path.basename()<\/code>\u51fd\u6570\u83b7\u53d6\u6587\u4ef6\u540d\uff0c\u5e76\u5c06\u7ed3\u679c\u5206\u522b\u5b58\u50a8\u5728<code>file_directory<\/code>\u548c<code>file_name<\/code>\u53d8\u91cf\u4e2d\uff0c\u6700\u540e\u6253\u5370\u6587\u4ef6\u8def\u5f84\u548c\u6587\u4ef6\u540d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<h2><strong>\u83b7\u53d6\u6587\u4ef6\u8def\u5f84\u548c\u6587\u4ef6\u540d<\/strong><\/h2>\n<p>file_path = Path(&#39;\/path\/to\/your\/file.txt&#39;)<\/p>\n<p>file_directory = file_path.parent<\/p>\n<p>file_name = file_path.name<\/p>\n<p>print(f&quot;\u6587\u4ef6\u8def\u5f84\u662f: {file_directory}, \u6587\u4ef6\u540d\u662f: {file_name}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528Pathlib\u6a21\u5757\u7684Path\u7c7b\u7684<code>parent<\/code>\u5c5e\u6027\u83b7\u53d6\u6587\u4ef6\u8def\u5f84\uff0c\u4f7f\u7528<code>name<\/code>\u5c5e\u6027\u83b7\u53d6\u6587\u4ef6\u540d\uff0c\u5e76\u5c06\u7ed3\u679c\u5206\u522b\u5b58\u50a8\u5728<code>file_directory<\/code>\u548c<code>file_name<\/code>\u53d8\u91cf\u4e2d\uff0c\u6700\u540e\u6253\u5370\u6587\u4ef6\u8def\u5f84\u548c\u6587\u4ef6\u540d\u3002<\/p>\n<\/p>\n<p><p><strong>\u5904\u7406\u5e26\u6709\u8def\u5f84\u7684\u6587\u4ef6\u540d\u7684\u65b9\u6cd5\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5728\u8fdb\u884c\u6587\u4ef6\u64cd\u4f5c\u65f6\u66f4\u65b9\u4fbf\u5730\u83b7\u53d6\u6587\u4ef6\u6240\u5728\u7684\u76ee\u5f55\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4e03\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u4ecb\u7ecd\u4e86Python\u83b7\u53d6\u6587\u4ef6\u540d\u5b57\u7b26\u4e32\u7684\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528os\u6a21\u5757\u3001Pathlib\u6a21\u5757\u3001glob\u6a21\u5757\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u7b49\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u4f18\u70b9\u548c\u9002\u7528\u573a\u666f\uff0c\u8bfb\u8005\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p><strong>os\u6a21\u5757\u9002\u5408\u5904\u7406\u7b80\u5355\u7684\u6587\u4ef6\u8def\u5f84\u64cd\u4f5c\uff0c\u517c\u5bb9\u6027\u597d\uff1bPathlib\u6a21\u5757\u63d0\u4f9b\u4e86\u9762\u5411\u5bf9\u8c61\u7684\u65b9\u5f0f\uff0c\u9002\u5408\u5904\u7406\u590d\u6742\u7684\u8def\u5f84\u64cd\u4f5c\uff1bglob\u6a21\u5757\u9002\u5408\u8fdb\u884c\u6a21\u5f0f\u5339\u914d\u548c\u6279\u91cf\u5904\u7406\u6587\u4ef6\uff1b\u6b63\u5219\u8868\u8fbe\u5f0f\u9002\u5408\u5904\u7406\u590d\u6742\u7684\u6587\u4ef6\u8def\u5f84\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u5e0c\u671b\u672c\u6587\u80fd\u591f\u5e2e\u52a9\u8bfb\u8005\u66f4\u597d\u5730\u7406\u89e3\u548c\u638c\u63e1Python\u83b7\u53d6\u6587\u4ef6\u540d\u5b57\u7b26\u4e32\u7684\u65b9\u6cd5\uff0c\u5e76\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u7075\u6d3b\u8fd0\u7528\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u63d0\u53d6\u6587\u4ef6\u540d\u800c\u4e0d\u5305\u542b\u6269\u5c55\u540d\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>os.path.splitext()<\/code>\u51fd\u6570\u6765\u5206\u79bb\u6587\u4ef6\u540d\u548c\u6269\u5c55\u540d\u3002\u9996\u5148\uff0c\u4f7f\u7528<code>os.path.basename()<\/code>\u83b7\u53d6\u5b8c\u6574\u7684\u6587\u4ef6\u540d\uff0c\u7136\u540e\u901a\u8fc7<code>os.path.splitext()<\/code>\u53bb\u6389\u6269\u5c55\u540d\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">import os\n\nfile_path = &#39;\/path\/to\/your\/file.txt&#39;\nfile_name, _ = os.path.splitext(os.path.basename(file_path))\nprint(file_name)  # \u8f93\u51fa: file\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c\u4f60\u5c31\u80fd\u5f97\u5230\u6ca1\u6709\u6269\u5c55\u540d\u7684\u6587\u4ef6\u540d\u5b57\u7b26\u4e32\u3002<\/p>\n<p><strong>\u4f7f\u7528Python\u83b7\u53d6\u6587\u4ef6\u540d\u65f6\uff0c\u53ef\u4ee5\u5904\u7406\u54ea\u4e9b\u7279\u6b8a\u5b57\u7b26\uff1f<\/strong><br \/>\u5728\u5904\u7406\u6587\u4ef6\u540d\u65f6\uff0c\u7279\u6b8a\u5b57\u7b26\uff08\u5982\u7a7a\u683c\u3001\u7b26\u53f7\u7b49\uff09\u53ef\u80fd\u4f1a\u5f15\u53d1\u95ee\u9898\u3002\u4e3a\u4e86\u5b89\u5168\u5730\u5904\u7406\u6587\u4ef6\u540d\uff0c\u5efa\u8bae\u4f7f\u7528<code>os.path<\/code>\u6a21\u5757\u4e2d\u7684<code>os.path.normpath()<\/code>\u6765\u6807\u51c6\u5316\u8def\u5f84\uff0c\u5e76\u4f7f\u7528<code>os.path.basename()<\/code>\u6765\u63d0\u53d6\u6587\u4ef6\u540d\u3002\u6b64\u5916\uff0c\u786e\u4fdd\u5bf9\u7528\u6237\u8f93\u5165\u8fdb\u884c\u9002\u5f53\u7684\u9a8c\u8bc1\u548c\u6e05\u7406\uff0c\u4ee5\u9632\u6b62\u6f5c\u5728\u7684\u5b89\u5168\u98ce\u9669\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u83b7\u53d6\u6587\u4ef6\u8def\u5f84\u7684\u6587\u4ef6\u540d\u90e8\u5206\uff1f<\/strong><br \/>\u4e3a\u4e86\u4ece\u5b8c\u6574\u7684\u6587\u4ef6\u8def\u5f84\u4e2d\u63d0\u53d6\u6587\u4ef6\u540d\u90e8\u5206\uff0c\u53ef\u4ee5\u4f7f\u7528<code>os.path.basename()<\/code>\u51fd\u6570\u3002\u8fd9\u4e2a\u51fd\u6570\u80fd\u591f\u8fd4\u56de\u8def\u5f84\u4e2d\u7684\u6700\u540e\u4e00\u90e8\u5206\uff0c\u65e0\u8bba\u8def\u5f84\u662f\u7edd\u5bf9\u8def\u5f84\u8fd8\u662f\u76f8\u5bf9\u8def\u5f84\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">import os\n\nfile_path = &#39;\/path\/to\/your\/file.txt&#39;\nfile_name = os.path.basename(file_path)\nprint(file_name)  # \u8f93\u51fa: file.txt\n<\/code><\/pre>\n<p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u60a8\u53ef\u4ee5\u8f7b\u677e\u83b7\u53d6\u8def\u5f84\u4e2d\u7684\u6587\u4ef6\u540d\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u83b7\u53d6\u6587\u4ef6\u540d\u5b57\u7b26\u4e32\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u4e3b\u8981\u5305\u62ec\u4f7f\u7528os\u6a21\u5757\u3001Pathlib\u6a21\u5757\u3001glob\u6a21\u5757\u7b49\u3002\u5176\u4e2d\u6700\u5e38\u7528 [&hellip;]","protected":false},"author":3,"featured_media":1109808,"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\/1109802"}],"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=1109802"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1109802\/revisions"}],"predecessor-version":[{"id":1109812,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1109802\/revisions\/1109812"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1109808"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1109802"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1109802"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1109802"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}