{"id":1180332,"date":"2025-01-15T18:35:29","date_gmt":"2025-01-15T10:35:29","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1180332.html"},"modified":"2025-01-15T18:35:31","modified_gmt":"2025-01-15T10:35:31","slug":"python%e5%a6%82%e4%bd%95%e6%8f%90%e5%8f%96%e6%96%87%e4%bb%b6%e7%9a%84%e8%b7%af%e5%be%84","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1180332.html","title":{"rendered":"python\u5982\u4f55\u63d0\u53d6\u6587\u4ef6\u7684\u8def\u5f84"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25114514\/0902df08-8db4-42c7-a5f0-34f5fbe0f035.webp\" alt=\"python\u5982\u4f55\u63d0\u53d6\u6587\u4ef6\u7684\u8def\u5f84\" \/><\/p>\n<p><p> \u5728Python\u4e2d\u63d0\u53d6\u6587\u4ef6\u8def\u5f84\u7684\u5e38\u7528\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528<code>os<\/code>\u6a21\u5757\u548c<code>pathlib<\/code>\u6a21\u5757\u3002<strong>\u53ef\u4ee5\u4f7f\u7528os.path\u6a21\u5757\u63d0\u53d6\u6587\u4ef6\u7684\u7edd\u5bf9\u8def\u5f84\u3001\u76f8\u5bf9\u8def\u5f84\u3001\u6587\u4ef6\u540d\u3001\u6587\u4ef6\u6269\u5c55\u540d\u7b49<\/strong>\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e24\u4e2a\u6a21\u5757\u6765\u63d0\u53d6\u6587\u4ef6\u8def\u5f84\uff0c\u5e76\u5c55\u793a\u5176\u5404\u81ea\u7684\u4f18\u7f3a\u70b9\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001os \u6a21\u5757<\/h3>\n<\/p>\n<p><p><code>os<\/code>\u6a21\u5757\u662fPython\u6807\u51c6\u5e93\u4e2d\u7684\u4e00\u4e2a\u6a21\u5757\uff0c\u63d0\u4f9b\u4e86\u4e0e\u64cd\u4f5c\u7cfb\u7edf\u8fdb\u884c\u4ea4\u4e92\u7684\u529f\u80fd\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u4f7f\u7528<code>os.path<\/code>\u6a21\u5757\u6765\u63d0\u53d6\u6587\u4ef6\u8def\u5f84\u7684\u8be6\u7ec6\u6b65\u9aa4\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u63d0\u53d6\u7edd\u5bf9\u8def\u5f84<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>os.path.abspath<\/code>\u65b9\u6cd5\u63d0\u53d6\u6587\u4ef6\u7684\u7edd\u5bf9\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>file_path = &quot;example.txt&quot;<\/p>\n<p>absolute_path = os.path.abspath(file_path)<\/p>\n<p>print(f&quot;Absolute Path: {absolute_path}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u63d0\u53d6\u6587\u4ef6\u540d<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>os.path.basename<\/code>\u65b9\u6cd5\u63d0\u53d6\u6587\u4ef6\u540d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>file_path = &quot;\/path\/to\/example.txt&quot;<\/p>\n<p>file_name = os.path.basename(file_path)<\/p>\n<p>print(f&quot;File Name: {file_name}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u63d0\u53d6\u76ee\u5f55\u540d<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>os.path.dirname<\/code>\u65b9\u6cd5\u63d0\u53d6\u76ee\u5f55\u540d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>file_path = &quot;\/path\/to\/example.txt&quot;<\/p>\n<p>directory_name = os.path.dirname(file_path)<\/p>\n<p>print(f&quot;Directory Name: {directory_name}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4\u3001\u63d0\u53d6\u6587\u4ef6\u6269\u5c55\u540d<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>os.path.splitext<\/code>\u65b9\u6cd5\u63d0\u53d6\u6587\u4ef6\u6269\u5c55\u540d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>file_path = &quot;\/path\/to\/example.txt&quot;<\/p>\n<p>file_name, file_extension = os.path.splitext(file_path)<\/p>\n<p>print(f&quot;File Extension: {file_extension}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001pathlib \u6a21\u5757<\/h3>\n<\/p>\n<p><p><code>pathlib<\/code>\u6a21\u5757\u662fPython 3.4\u5f15\u5165\u7684\u65b0\u6a21\u5757\uff0c\u63d0\u4f9b\u4e86\u9762\u5411\u5bf9\u8c61\u7684\u8def\u5f84\u64cd\u4f5c\u65b9\u6cd5\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u4f7f\u7528<code>pathlib<\/code>\u6a21\u5757\u6765\u63d0\u53d6\u6587\u4ef6\u8def\u5f84\u7684\u8be6\u7ec6\u6b65\u9aa4\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u63d0\u53d6\u7edd\u5bf9\u8def\u5f84<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>Path.resolve<\/code>\u65b9\u6cd5\u63d0\u53d6\u6587\u4ef6\u7684\u7edd\u5bf9\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<p>file_path = Path(&quot;example.txt&quot;)<\/p>\n<p>absolute_path = file_path.resolve()<\/p>\n<p>print(f&quot;Absolute Path: {absolute_path}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u63d0\u53d6\u6587\u4ef6\u540d<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>Path.name<\/code>\u5c5e\u6027\u63d0\u53d6\u6587\u4ef6\u540d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<p>file_path = Path(&quot;\/path\/to\/example.txt&quot;)<\/p>\n<p>file_name = file_path.name<\/p>\n<p>print(f&quot;File Name: {file_name}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u63d0\u53d6\u76ee\u5f55\u540d<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>Path.parent<\/code>\u5c5e\u6027\u63d0\u53d6\u76ee\u5f55\u540d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<p>file_path = Path(&quot;\/path\/to\/example.txt&quot;)<\/p>\n<p>directory_name = file_path.parent<\/p>\n<p>print(f&quot;Directory Name: {directory_name}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4\u3001\u63d0\u53d6\u6587\u4ef6\u6269\u5c55\u540d<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>Path.suffix<\/code>\u5c5e\u6027\u63d0\u53d6\u6587\u4ef6\u6269\u5c55\u540d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<p>file_path = Path(&quot;\/path\/to\/example.txt&quot;)<\/p>\n<p>file_extension = file_path.suffix<\/p>\n<p>print(f&quot;File Extension: {file_extension}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001os \u6a21\u5757\u4e0e pathlib \u6a21\u5757\u7684\u5bf9\u6bd4<\/h3>\n<\/p>\n<p><h4>1\u3001os \u6a21\u5757\u7684\u4f18\u70b9<\/h4>\n<\/p>\n<ul>\n<li><strong>\u517c\u5bb9\u6027\u597d<\/strong>\uff1a<code>os<\/code>\u6a21\u5757\u5728\u6240\u6709\u652f\u6301Python\u7684\u64cd\u4f5c\u7cfb\u7edf\u4e0a\u90fd\u80fd\u6b63\u5e38\u5de5\u4f5c\u3002<\/li>\n<li><strong>\u529f\u80fd\u5168\u9762<\/strong>\uff1a<code>os<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u5927\u91cf\u7684\u4e0e\u64cd\u4f5c\u7cfb\u7edf\u4ea4\u4e92\u7684\u51fd\u6570\uff0c\u53ef\u4ee5\u5904\u7406\u5404\u79cd\u6587\u4ef6\u548c\u76ee\u5f55\u64cd\u4f5c\u3002<\/li>\n<\/ul>\n<p><h4>2\u3001os \u6a21\u5757\u7684\u7f3a\u70b9<\/h4>\n<\/p>\n<ul>\n<li><strong>\u4e0d\u591f\u76f4\u89c2<\/strong>\uff1a<code>os<\/code>\u6a21\u5757\u7684\u51fd\u6570\u540d\u548c\u53c2\u6570\u6709\u65f6\u4e0d\u591f\u76f4\u89c2\uff0c\u9700\u8981\u67e5\u9605\u6587\u6863\u624d\u80fd\u7406\u89e3\u5176\u7528\u6cd5\u3002<\/li>\n<li><strong>\u7f3a\u4e4f\u9762\u5411\u5bf9\u8c61<\/strong>\uff1a<code>os<\/code>\u6a21\u5757\u7684\u51fd\u6570\u90fd\u662f\u9762\u5411\u8fc7\u7a0b\u7684\uff0c\u4e0d\u652f\u6301\u9762\u5411\u5bf9\u8c61\u7684\u7f16\u7a0b\u98ce\u683c\u3002<\/li>\n<\/ul>\n<p><h4>3\u3001pathlib \u6a21\u5757\u7684\u4f18\u70b9<\/h4>\n<\/p>\n<ul>\n<li><strong>\u9762\u5411\u5bf9\u8c61<\/strong>\uff1a<code>pathlib<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u9762\u5411\u5bf9\u8c61\u7684\u8def\u5f84\u64cd\u4f5c\u65b9\u6cd5\uff0c\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u548c\u76f4\u89c2\u3002<\/li>\n<li><strong>\u529f\u80fd\u5f3a\u5927<\/strong>\uff1a<code>pathlib<\/code>\u6a21\u5757\u4e0d\u4ec5\u652f\u6301\u8def\u5f84\u64cd\u4f5c\uff0c\u8fd8\u652f\u6301\u6587\u4ef6I\/O\u64cd\u4f5c\uff0c\u529f\u80fd\u975e\u5e38\u5f3a\u5927\u3002<\/li>\n<li><strong>\u6613\u4e8e\u9605\u8bfb<\/strong>\uff1a<code>pathlib<\/code>\u6a21\u5757\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\u540d\u975e\u5e38\u76f4\u89c2\uff0c\u4ee3\u7801\u6613\u4e8e\u9605\u8bfb\u548c\u7406\u89e3\u3002<\/li>\n<\/ul>\n<p><h4>4\u3001pathlib \u6a21\u5757\u7684\u7f3a\u70b9<\/h4>\n<\/p>\n<ul>\n<li><strong>\u517c\u5bb9\u6027\u5dee<\/strong>\uff1a<code>pathlib<\/code>\u6a21\u5757\u662fPython 3.4\u5f15\u5165\u7684\u65b0\u6a21\u5757\uff0c\u4e0d\u652f\u6301Python 2.x\u7248\u672c\u3002<\/li>\n<li><strong>\u5b66\u4e60\u6210\u672c<\/strong>\uff1a\u5bf9\u4e60\u60ef\u4e86<code>os<\/code>\u6a21\u5757\u7684\u5f00\u53d1\u8005\u6765\u8bf4\uff0c\u5b66\u4e60\u548c\u4f7f\u7528<code>pathlib<\/code>\u6a21\u5757\u9700\u8981\u4e00\u5b9a\u7684\u65f6\u95f4\u548c\u7cbe\u529b\u3002<\/li>\n<\/ul>\n<p><h3>\u56db\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u4ecb\u7ecd\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\uff0c<strong>Python\u4e2d\u63d0\u53d6\u6587\u4ef6\u8def\u5f84\u7684\u5e38\u7528\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528os\u6a21\u5757\u548cpathlib\u6a21\u5757<\/strong>\u3002<code>os<\/code>\u6a21\u5757\u529f\u80fd\u5168\u9762\uff0c\u517c\u5bb9\u6027\u597d\uff0c\u4f46\u7f3a\u4e4f\u9762\u5411\u5bf9\u8c61\u652f\u6301\uff1b<code>pathlib<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u9762\u5411\u5bf9\u8c61\u7684\u8def\u5f84\u64cd\u4f5c\u65b9\u6cd5\uff0c\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u548c\u76f4\u89c2\uff0c\u4f46\u517c\u5bb9\u6027\u8f83\u5dee\u3002\u5f00\u53d1\u8005\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u6a21\u5757\u6765\u5904\u7406\u6587\u4ef6\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u5b9e\u4f8b\u5e94\u7528<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u6211\u4eec\u7ecf\u5e38\u9700\u8981\u5904\u7406\u6587\u4ef6\u8def\u5f84\uff0c\u4e0b\u9762\u901a\u8fc7\u51e0\u4e2a\u5b9e\u4f8b\u6765\u5c55\u793a\u5982\u4f55\u4f7f\u7528<code>os<\/code>\u6a21\u5757\u548c<code>pathlib<\/code>\u6a21\u5757\u5904\u7406\u6587\u4ef6\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u83b7\u53d6\u5f53\u524d\u811a\u672c\u6240\u5728\u76ee\u5f55<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>os.path<\/code>\u6a21\u5757\u4e2d\u7684<code>__file__<\/code>\u53d8\u91cf\u548c<code>os.path.dirname<\/code>\u65b9\u6cd5\u6765\u83b7\u53d6\u5f53\u524d\u811a\u672c\u6240\u5728\u76ee\u5f55\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>current_script_directory = os.path.dirname(os.path.abspath(__file__))<\/p>\n<p>print(f&quot;Current Script Directory: {current_script_directory}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e5f\u53ef\u4ee5\u4f7f\u7528<code>pathlib<\/code>\u6a21\u5757\u4e2d\u7684<code>Path<\/code>\u7c7b\u6765\u83b7\u53d6\u5f53\u524d\u811a\u672c\u6240\u5728\u76ee\u5f55\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<p>current_script_directory = Path(__file__).parent.resolve()<\/p>\n<p>print(f&quot;Current Script Directory: {current_script_directory}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u83b7\u53d6\u6307\u5b9a\u76ee\u5f55\u4e0b\u7684\u6240\u6709\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>os<\/code>\u6a21\u5757\u4e2d\u7684<code>os.listdir<\/code>\u65b9\u6cd5\u6765\u83b7\u53d6\u6307\u5b9a\u76ee\u5f55\u4e0b\u7684\u6240\u6709\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>directory_path = &quot;\/path\/to\/directory&quot;<\/p>\n<p>files = os.listdir(directory_path)<\/p>\n<p>print(f&quot;Files in Directory: {files}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e5f\u53ef\u4ee5\u4f7f\u7528<code>pathlib<\/code>\u6a21\u5757\u4e2d\u7684<code>Path.iterdir<\/code>\u65b9\u6cd5\u6765\u83b7\u53d6\u6307\u5b9a\u76ee\u5f55\u4e0b\u7684\u6240\u6709\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<p>directory_path = Path(&quot;\/path\/to\/directory&quot;)<\/p>\n<p>files = [f for f in directory_path.iterdir() if f.is_file()]<\/p>\n<p>print(f&quot;Files in Directory: {files}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u68c0\u67e5\u6587\u4ef6\u662f\u5426\u5b58\u5728<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>os.path<\/code>\u6a21\u5757\u4e2d\u7684<code>os.path.exists<\/code>\u65b9\u6cd5\u6765\u68c0\u67e5\u6587\u4ef6\u662f\u5426\u5b58\u5728\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>file_path = &quot;\/path\/to\/file.txt&quot;<\/p>\n<p>file_exists = os.path.exists(file_path)<\/p>\n<p>print(f&quot;File Exists: {file_exists}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e5f\u53ef\u4ee5\u4f7f\u7528<code>pathlib<\/code>\u6a21\u5757\u4e2d\u7684<code>Path.exists<\/code>\u65b9\u6cd5\u6765\u68c0\u67e5\u6587\u4ef6\u662f\u5426\u5b58\u5728\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<p>file_path = Path(&quot;\/path\/to\/file.txt&quot;)<\/p>\n<p>file_exists = file_path.exists()<\/p>\n<p>print(f&quot;File Exists: {file_exists}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u6269\u5c55\u5185\u5bb9<\/h3>\n<\/p>\n<p><h4>1\u3001\u8def\u5f84\u62fc\u63a5<\/h4>\n<\/p>\n<p><p>\u5728\u5904\u7406\u6587\u4ef6\u8def\u5f84\u65f6\uff0c\u7ecf\u5e38\u9700\u8981\u62fc\u63a5\u591a\u4e2a\u8def\u5f84\uff0c\u53ef\u4ee5\u4f7f\u7528<code>os.path.join<\/code>\u65b9\u6cd5\u8fdb\u884c\u8def\u5f84\u62fc\u63a5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>base_path = &quot;\/path\/to&quot;<\/p>\n<p>file_name = &quot;example.txt&quot;<\/p>\n<p>full_path = os.path.join(base_path, file_name)<\/p>\n<p>print(f&quot;Full Path: {full_path}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e5f\u53ef\u4ee5\u4f7f\u7528<code>pathlib<\/code>\u6a21\u5757\u4e2d\u7684<code>Path<\/code>\u7c7b\u8fdb\u884c\u8def\u5f84\u62fc\u63a5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<p>base_path = Path(&quot;\/path\/to&quot;)<\/p>\n<p>file_name = &quot;example.txt&quot;<\/p>\n<p>full_path = base_path \/ file_name<\/p>\n<p>print(f&quot;Full Path: {full_path}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u5904\u7406\u76f8\u5bf9\u8def\u5f84<\/h4>\n<\/p>\n<p><p>\u5728\u5904\u7406\u76f8\u5bf9\u8def\u5f84\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>os.path.abspath<\/code>\u65b9\u6cd5\u5c06\u76f8\u5bf9\u8def\u5f84\u8f6c\u6362\u4e3a\u7edd\u5bf9\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>relative_path = &quot;..\/example.txt&quot;<\/p>\n<p>absolute_path = os.path.abspath(relative_path)<\/p>\n<p>print(f&quot;Absolute Path: {absolute_path}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e5f\u53ef\u4ee5\u4f7f\u7528<code>pathlib<\/code>\u6a21\u5757\u4e2d\u7684<code>Path.resolve<\/code>\u65b9\u6cd5\u5c06\u76f8\u5bf9\u8def\u5f84\u8f6c\u6362\u4e3a\u7edd\u5bf9\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<p>relative_path = Path(&quot;..\/example.txt&quot;)<\/p>\n<p>absolute_path = relative_path.resolve()<\/p>\n<p>print(f&quot;Absolute Path: {absolute_path}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u83b7\u53d6\u6587\u4ef6\u5927\u5c0f<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>os.path.getsize<\/code>\u65b9\u6cd5\u83b7\u53d6\u6587\u4ef6\u7684\u5927\u5c0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>file_path = &quot;\/path\/to\/example.txt&quot;<\/p>\n<p>file_size = os.path.getsize(file_path)<\/p>\n<p>print(f&quot;File Size: {file_size} bytes&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e5f\u53ef\u4ee5\u4f7f\u7528<code>pathlib<\/code>\u6a21\u5757\u4e2d\u7684<code>Path.stat<\/code>\u65b9\u6cd5\u83b7\u53d6\u6587\u4ef6\u7684\u5927\u5c0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<p>file_path = Path(&quot;\/path\/to\/example.txt&quot;)<\/p>\n<p>file_size = file_path.stat().st_size<\/p>\n<p>print(f&quot;File Size: {file_size} bytes&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u5b9e\u9645\u6848\u4f8b<\/h3>\n<\/p>\n<p><h4>1\u3001\u6587\u4ef6\u5907\u4efd\u811a\u672c<\/h4>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u6587\u4ef6\u5907\u4efd\u811a\u672c\uff0c\u4f7f\u7528<code>os<\/code>\u6a21\u5757\u548c<code>pathlib<\/code>\u6a21\u5757\u5b9e\u73b0\u6587\u4ef6\u5907\u4efd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>import shutil<\/p>\n<p>from pathlib import Path<\/p>\n<p>def backup_file_os(file_path, backup_directory):<\/p>\n<p>    if not os.path.exists(backup_directory):<\/p>\n<p>        os.makedirs(backup_directory)<\/p>\n<p>    file_name = os.path.basename(file_path)<\/p>\n<p>    backup_path = os.path.join(backup_directory, file_name)<\/p>\n<p>    shutil.copy(file_path, backup_path)<\/p>\n<p>    print(f&quot;File backed up to: {backup_path}&quot;)<\/p>\n<p>def backup_file_pathlib(file_path, backup_directory):<\/p>\n<p>    backup_directory = Path(backup_directory)<\/p>\n<p>    if not backup_directory.exists():<\/p>\n<p>        backup_directory.mkdir(parents=True)<\/p>\n<p>    file_path = Path(file_path)<\/p>\n<p>    backup_path = backup_directory \/ file_path.name<\/p>\n<p>    shutil.copy(file_path, backup_path)<\/p>\n<p>    print(f&quot;File backed up to: {backup_path}&quot;)<\/p>\n<p>file_path = &quot;\/path\/to\/example.txt&quot;<\/p>\n<p>backup_directory = &quot;\/path\/to\/backup&quot;<\/p>\n<p>backup_file_os(file_path, backup_directory)<\/p>\n<p>backup_file_pathlib(file_path, backup_directory)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u65e5\u5fd7\u6587\u4ef6\u5904\u7406\u811a\u672c<\/h4>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u65e5\u5fd7\u6587\u4ef6\u5904\u7406\u811a\u672c\uff0c\u4f7f\u7528<code>os<\/code>\u6a21\u5757\u548c<code>pathlib<\/code>\u6a21\u5757\u5b9e\u73b0\u65e5\u5fd7\u6587\u4ef6\u7684\u5f52\u6863\u548c\u6e05\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>import shutil<\/p>\n<p>from datetime import datetime<\/p>\n<p>from pathlib import Path<\/p>\n<p>def archive_logs_os(log_directory, archive_directory, days):<\/p>\n<p>    if not os.path.exists(archive_directory):<\/p>\n<p>        os.makedirs(archive_directory)<\/p>\n<p>    for file_name in os.listdir(log_directory):<\/p>\n<p>        file_path = os.path.join(log_directory, file_name)<\/p>\n<p>        if os.path.isfile(file_path):<\/p>\n<p>            file_mtime = os.path.getmtime(file_path)<\/p>\n<p>            file_date = datetime.fromtimestamp(file_mtime)<\/p>\n<p>            if (datetime.now() - file_date).days &gt; days:<\/p>\n<p>                archive_path = os.path.join(archive_directory, file_name)<\/p>\n<p>                shutil.move(file_path, archive_path)<\/p>\n<p>                print(f&quot;Archived: {file_path} to {archive_path}&quot;)<\/p>\n<p>def archive_logs_pathlib(log_directory, archive_directory, days):<\/p>\n<p>    log_directory = Path(log_directory)<\/p>\n<p>    archive_directory = Path(archive_directory)<\/p>\n<p>    if not archive_directory.exists():<\/p>\n<p>        archive_directory.mkdir(parents=True)<\/p>\n<p>    for file_path in log_directory.iterdir():<\/p>\n<p>        if file_path.is_file():<\/p>\n<p>            file_date = datetime.fromtimestamp(file_path.stat().st_mtime)<\/p>\n<p>            if (datetime.now() - file_date).days &gt; days:<\/p>\n<p>                archive_path = archive_directory \/ file_path.name<\/p>\n<p>                shutil.move(file_path, archive_path)<\/p>\n<p>                print(f&quot;Archived: {file_path} to {archive_path}&quot;)<\/p>\n<p>log_directory = &quot;\/path\/to\/logs&quot;<\/p>\n<p>archive_directory = &quot;\/path\/to\/archive&quot;<\/p>\n<p>days = 30<\/p>\n<p>archive_logs_os(log_directory, archive_directory, days)<\/p>\n<p>archive_logs_pathlib(log_directory, archive_directory, days)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001\u7ed3\u8bba<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u6211\u4eec\u4e86\u89e3\u4e86<strong>Python\u4e2d\u63d0\u53d6\u6587\u4ef6\u8def\u5f84\u7684\u5e38\u7528\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528os\u6a21\u5757\u548cpathlib\u6a21\u5757<\/strong>\uff0c\u5e76\u8be6\u7ec6\u8bb2\u89e3\u4e86\u5982\u4f55\u4f7f\u7528\u8fd9\u4e24\u4e2a\u6a21\u5757\u6765\u5904\u7406\u6587\u4ef6\u8def\u5f84\u3002\u6211\u4eec\u8fd8\u901a\u8fc7\u5b9e\u9645\u6848\u4f8b\u5c55\u793a\u4e86\u5982\u4f55\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\u5e94\u7528\u8fd9\u4e9b\u65b9\u6cd5\u3002\u5e0c\u671b\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u80fd\u591f\u5e2e\u52a9\u8bfb\u8005\u66f4\u597d\u5730\u7406\u89e3\u548c\u638c\u63e1Python\u4e2d\u6587\u4ef6\u8def\u5f84\u7684\u5904\u7406\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u83b7\u53d6\u6587\u4ef6\u7684\u7edd\u5bf9\u8def\u5f84\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>os.path.abspath()<\/code>\u51fd\u6570\u6765\u83b7\u53d6\u6587\u4ef6\u7684\u7edd\u5bf9\u8def\u5f84\u3002\u53ea\u9700\u5c06\u6587\u4ef6\u540d\u6216\u76f8\u5bf9\u8def\u5f84\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u6b64\u51fd\u6570\uff0c\u5373\u53ef\u83b7\u5f97\u5b8c\u6574\u7684\u6587\u4ef6\u8def\u5f84\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import os\n\nfile_name = &#39;example.txt&#39;\nabsolute_path = os.path.abspath(file_name)\nprint(absolute_path)\n<\/code><\/pre>\n<p><strong>\u5982\u4f55\u4ece\u6587\u4ef6\u8def\u5f84\u4e2d\u63d0\u53d6\u6587\u4ef6\u540d\u548c\u6269\u5c55\u540d\uff1f<\/strong><br \/>\u8981\u4ece\u6587\u4ef6\u8def\u5f84\u4e2d\u63d0\u53d6\u6587\u4ef6\u540d\u548c\u6269\u5c55\u540d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>os.path.basename()<\/code>\u548c<code>os.path.splitext()<\/code>\u8fd9\u4e24\u4e2a\u51fd\u6570\u3002\u524d\u8005\u7528\u4e8e\u83b7\u53d6\u6587\u4ef6\u540d\uff0c\u540e\u8005\u7528\u4e8e\u5206\u79bb\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\nfile_path = &#39;\/path\/to\/example.txt&#39;\nfile_name = os.path.basename(file_path)  # \u63d0\u53d6\u6587\u4ef6\u540d\nfile_root, file_extension = os.path.splitext(file_name)  # \u5206\u79bb\u6587\u4ef6\u540d\u548c\u6269\u5c55\u540d\nprint(file_name, file_root, file_extension)\n<\/code><\/pre>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u68c0\u67e5\u8def\u5f84\u662f\u5426\u5b58\u5728\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528<code>os.path.exists()<\/code>\u51fd\u6570\u6765\u68c0\u67e5\u7ed9\u5b9a\u8def\u5f84\u662f\u5426\u5b58\u5728\u3002\u6b64\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u5e03\u5c14\u503c\uff0c\u6307\u793a\u8def\u5f84\u662f\u5426\u6709\u6548\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import os\n\npath = &#39;\/path\/to\/example.txt&#39;\nif os.path.exists(path):\n    print(&quot;\u8def\u5f84\u5b58\u5728&quot;)\nelse:\n    print(&quot;\u8def\u5f84\u4e0d\u5b58\u5728&quot;)\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u63d0\u53d6\u6587\u4ef6\u8def\u5f84\u7684\u5e38\u7528\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528os\u6a21\u5757\u548cpathlib\u6a21\u5757\u3002\u53ef\u4ee5\u4f7f\u7528os.path\u6a21\u5757\u63d0\u53d6\u6587\u4ef6 [&hellip;]","protected":false},"author":3,"featured_media":1180340,"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\/1180332"}],"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=1180332"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1180332\/revisions"}],"predecessor-version":[{"id":1180342,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1180332\/revisions\/1180342"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1180340"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1180332"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1180332"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1180332"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}