{"id":1171022,"date":"2025-01-15T16:30:02","date_gmt":"2025-01-15T08:30:02","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1171022.html"},"modified":"2025-01-15T16:30:06","modified_gmt":"2025-01-15T08:30:06","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e8%ae%bf%e9%97%aec%e7%9b%98","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1171022.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u8bbf\u95eec\u76d8"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26073447\/c3a50834-936d-4fe2-aa0e-d428069b5549.webp\" alt=\"python\u4e2d\u5982\u4f55\u8bbf\u95eec\u76d8\" \/><\/p>\n<p><p> \u5728Python\u4e2d\uff0c\u8bbf\u95eeC\u76d8\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff0c<strong>\u4f7f\u7528\u7edd\u5bf9\u8def\u5f84\u3001\u4f7f\u7528\u76f8\u5bf9\u8def\u5f84\u3001\u4f7f\u7528os\u5e93\u3001\u4f7f\u7528pathlib\u5e93<\/strong>\u3002\u5176\u4e2d\uff0c\u4f7f\u7528<code>os<\/code>\u5e93\u662f\u4e00\u79cd\u5e38\u89c1\u4e14\u6709\u6548\u7684\u65b9\u6cd5\u3002\u63a5\u4e0b\u6765\u5c06\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u4f7f\u7528<code>os<\/code>\u5e93\u6765\u8bbf\u95eeC\u76d8\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528 <code>os<\/code> \u5e93\u8bbf\u95eeC\u76d8\uff1a<\/strong><\/p>\n<p><code>os<\/code>\u5e93\u662fPython\u6807\u51c6\u5e93\u7684\u4e00\u90e8\u5206\uff0c\u63d0\u4f9b\u4e86\u4e0e\u64cd\u4f5c\u7cfb\u7edf\u8fdb\u884c\u4ea4\u4e92\u7684\u5404\u79cd\u65b9\u6cd5\u3002\u901a\u8fc7<code>os<\/code>\u5e93\uff0c\u6211\u4eec\u53ef\u4ee5\u6267\u884c\u6587\u4ef6\u548c\u76ee\u5f55\u7684\u521b\u5efa\u3001\u5220\u9664\u3001\u8bfb\u53d6\u3001\u5199\u5165\u7b49\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u5bfc\u5165<code>os<\/code>\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528<code>os<\/code>\u5e93\u4e2d\u7684\u65b9\u6cd5\u8bbf\u95eeC\u76d8\u3002\u4f8b\u5982\uff0c\u5217\u51faC\u76d8\u4e2d\u7684\u6240\u6709\u6587\u4ef6\u548c\u76ee\u5f55\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">c_drive_contents = os.listdir(&#39;C:\\\\&#39;)<\/p>\n<p>print(c_drive_contents)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>os.listdir<\/code>\u65b9\u6cd5\u63a5\u53d7\u4e00\u4e2a\u8def\u5f84\u53c2\u6570\uff0c\u5e76\u8fd4\u56de\u8be5\u8def\u5f84\u4e0b\u6240\u6709\u6587\u4ef6\u548c\u76ee\u5f55\u7684\u5217\u8868\u3002\u6ce8\u610f\u5728\u8def\u5f84\u4e2d\u4f7f\u7528\u53cc\u53cd\u659c\u6760\uff08<code>\\<\/code>\uff09\u6765\u8f6c\u4e49\u53cd\u659c\u6760\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u7edd\u5bf9\u8def\u5f84\u548c\u76f8\u5bf9\u8def\u5f84<\/h3>\n<\/p>\n<p><p><strong>\u7edd\u5bf9\u8def\u5f84\uff1a<\/strong><\/p>\n<p>\u7edd\u5bf9\u8def\u5f84\u662f\u4ece\u6839\u76ee\u5f55\u5f00\u59cb\u7684\u8def\u5f84\u3002\u5728Windows\u7cfb\u7edf\u4e2d\uff0cC\u76d8\u7684\u6839\u76ee\u5f55\u8def\u5f84\u4e3a<code>C:<\/code>\u3002\u5728Python\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u7edd\u5bf9\u8def\u5f84\u8bbf\u95eeC\u76d8\u4e2d\u7684\u6587\u4ef6\u548c\u76ee\u5f55\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u8bbf\u95eeC\u76d8\u6839\u76ee\u5f55<\/strong><\/h2>\n<p>c_drive = &#39;C:\\\\&#39;<\/p>\n<p>files_in_c_drive = os.listdir(c_drive)<\/p>\n<p>print(files_in_c_drive)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>os.listdir<\/code>\u65b9\u6cd5\u5217\u51fa\u4e86C\u76d8\u6839\u76ee\u5f55\u4e2d\u7684\u6240\u6709\u6587\u4ef6\u548c\u76ee\u5f55\u3002<\/p>\n<\/p>\n<p><p><strong>\u76f8\u5bf9\u8def\u5f84\uff1a<\/strong><\/p>\n<p>\u76f8\u5bf9\u8def\u5f84\u662f\u76f8\u5bf9\u4e8e\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u7684\u8def\u5f84\u3002\u5728Python\u811a\u672c\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u76f8\u5bf9\u8def\u5f84\u8bbf\u95eeC\u76d8\u4e2d\u7684\u6587\u4ef6\u548c\u76ee\u5f55\u3002\u4f8b\u5982\uff0c\u5982\u679c\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u662f<code>C:\\Users\\Username<\/code>\uff0c\u90a3\u4e48\u53ef\u4ee5\u4f7f\u7528\u76f8\u5bf9\u8def\u5f84\u8bbf\u95eeC\u76d8\u4e2d\u7684\u5176\u4ed6\u76ee\u5f55\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u5047\u8bbe\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u662fC:\\Users\\Username<\/strong><\/h2>\n<p>relative_path = &#39;..\\\\..\\\\&#39;<\/p>\n<p>files_in_c_drive = os.listdir(relative_path)<\/p>\n<p>print(files_in_c_drive)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>..\\\\..\\<\/code>\u8868\u793a\u4ece\u5f53\u524d\u76ee\u5f55\u5411\u4e0a\u79fb\u52a8\u4e24\u7ea7\uff0c\u5373\u5230\u8fbeC\u76d8\u6839\u76ee\u5f55\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528 os \u5e93<\/h3>\n<\/p>\n<p><p><strong>\u83b7\u53d6\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>current_directory = os.getcwd()<\/p>\n<p>print(f&#39;Current Directory: {current_directory}&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>os.getcwd<\/code>\u65b9\u6cd5\u8fd4\u56de\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u7684\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><p><strong>\u6539\u53d8\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u6539\u53d8\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u5230C\u76d8\u6839\u76ee\u5f55<\/strong><\/h2>\n<p>os.chdir(&#39;C:\\\\&#39;)<\/p>\n<p>new_directory = os.getcwd()<\/p>\n<p>print(f&#39;New Directory: {new_directory}&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>os.chdir<\/code>\u65b9\u6cd5\u5c06\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u6539\u53d8\u4e3aC\u76d8\u6839\u76ee\u5f55\u3002<\/p>\n<\/p>\n<p><p><strong>\u521b\u5efa\u548c\u5220\u9664\u76ee\u5f55\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u5728C\u76d8\u6839\u76ee\u5f55\u521b\u5efa\u65b0\u76ee\u5f55<\/strong><\/h2>\n<p>new_dir_path = &#39;C:\\\\new_directory&#39;<\/p>\n<p>os.mkdir(new_dir_path)<\/p>\n<h2><strong>\u9a8c\u8bc1\u76ee\u5f55\u662f\u5426\u521b\u5efa<\/strong><\/h2>\n<p>if os.path.exists(new_dir_path):<\/p>\n<p>    print(&#39;Directory created successfully.&#39;)<\/p>\n<h2><strong>\u5220\u9664\u65b0\u521b\u5efa\u7684\u76ee\u5f55<\/strong><\/h2>\n<p>os.rmdir(new_dir_path)<\/p>\n<h2><strong>\u9a8c\u8bc1\u76ee\u5f55\u662f\u5426\u5220\u9664<\/strong><\/h2>\n<p>if not os.path.exists(new_dir_path):<\/p>\n<p>    print(&#39;Directory deleted successfully.&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>os.mkdir<\/code>\u65b9\u6cd5\u521b\u5efa\u4e00\u4e2a\u65b0\u76ee\u5f55\uff0c<code>os.rmdir<\/code>\u65b9\u6cd5\u5220\u9664\u8be5\u76ee\u5f55\u3002<\/p>\n<\/p>\n<p><p><strong>\u6587\u4ef6\u64cd\u4f5c\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u5728C\u76d8\u6839\u76ee\u5f55\u521b\u5efa\u65b0\u6587\u4ef6<\/strong><\/h2>\n<p>file_path = &#39;C:\\\\new_file.txt&#39;<\/p>\n<p>with open(file_path, &#39;w&#39;) as file:<\/p>\n<p>    file.write(&#39;Hello, C Drive!&#39;)<\/p>\n<h2><strong>\u9a8c\u8bc1\u6587\u4ef6\u662f\u5426\u521b\u5efa<\/strong><\/h2>\n<p>if os.path.exists(file_path):<\/p>\n<p>    print(&#39;File created successfully.&#39;)<\/p>\n<h2><strong>\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9<\/strong><\/h2>\n<p>with open(file_path, &#39;r&#39;) as file:<\/p>\n<p>    content = file.read()<\/p>\n<p>    print(f&#39;File Content: {content}&#39;)<\/p>\n<h2><strong>\u5220\u9664\u65b0\u521b\u5efa\u7684\u6587\u4ef6<\/strong><\/h2>\n<p>os.remove(file_path)<\/p>\n<h2><strong>\u9a8c\u8bc1\u6587\u4ef6\u662f\u5426\u5220\u9664<\/strong><\/h2>\n<p>if not os.path.exists(file_path):<\/p>\n<p>    print(&#39;File deleted successfully.&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5c55\u793a\u4e86\u5982\u4f55\u5728C\u76d8\u6839\u76ee\u5f55\u521b\u5efa\u3001\u8bfb\u53d6\u548c\u5220\u9664\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528 pathlib \u5e93<\/h3>\n<\/p>\n<p><p><code>pathlib<\/code>\u5e93\u662fPython 3.4\u5f15\u5165\u7684\u4e00\u4e2a\u6a21\u5757\uff0c\u63d0\u4f9b\u4e86\u4e00\u79cd\u9762\u5411\u5bf9\u8c61\u7684\u65b9\u6cd5\u6765\u5904\u7406\u6587\u4ef6\u7cfb\u7edf\u8def\u5f84\u3002\u76f8\u6bd4<code>os<\/code>\u5e93\uff0c<code>pathlib<\/code>\u5e93\u66f4\u52a0\u76f4\u89c2\u548c\u6613\u7528\u3002<\/p>\n<\/p>\n<p><p><strong>\u521b\u5efa Path \u5bf9\u8c61\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<h2><strong>\u521b\u5efaPath\u5bf9\u8c61\u8868\u793aC\u76d8\u6839\u76ee\u5f55<\/strong><\/h2>\n<p>c_drive = Path(&#39;C:\/&#39;)<\/p>\n<p>print(c_drive)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u8868\u793aC\u76d8\u6839\u76ee\u5f55\u7684Path\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><p><strong>\u5217\u51fa\u76ee\u5f55\u5185\u5bb9\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<h2><strong>\u5217\u51faC\u76d8\u6839\u76ee\u5f55\u4e2d\u7684\u6240\u6709\u6587\u4ef6\u548c\u76ee\u5f55<\/strong><\/h2>\n<p>c_drive = Path(&#39;C:\/&#39;)<\/p>\n<p>for item in c_drive.iterdir():<\/p>\n<p>    print(item)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>iterdir<\/code>\u65b9\u6cd5\u8fd4\u56deC\u76d8\u6839\u76ee\u5f55\u4e2d\u6240\u6709\u6587\u4ef6\u548c\u76ee\u5f55\u7684\u751f\u6210\u5668\u3002<\/p>\n<\/p>\n<p><p><strong>\u521b\u5efa\u548c\u5220\u9664\u76ee\u5f55\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<h2><strong>\u521b\u5efa\u65b0\u76ee\u5f55<\/strong><\/h2>\n<p>new_dir = Path(&#39;C:\/new_directory&#39;)<\/p>\n<p>new_dir.mkdir()<\/p>\n<h2><strong>\u9a8c\u8bc1\u76ee\u5f55\u662f\u5426\u521b\u5efa<\/strong><\/h2>\n<p>if new_dir.exists():<\/p>\n<p>    print(&#39;Directory created successfully.&#39;)<\/p>\n<h2><strong>\u5220\u9664\u65b0\u521b\u5efa\u7684\u76ee\u5f55<\/strong><\/h2>\n<p>new_dir.rmdir()<\/p>\n<h2><strong>\u9a8c\u8bc1\u76ee\u5f55\u662f\u5426\u5220\u9664<\/strong><\/h2>\n<p>if not new_dir.exists():<\/p>\n<p>    print(&#39;Directory deleted successfully.&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>mkdir<\/code>\u65b9\u6cd5\u521b\u5efa\u65b0\u76ee\u5f55\uff0c\u4f7f\u7528<code>rmdir<\/code>\u65b9\u6cd5\u5220\u9664\u8be5\u76ee\u5f55\u3002<\/p>\n<\/p>\n<p><p><strong>\u6587\u4ef6\u64cd\u4f5c\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<h2><strong>\u521b\u5efa\u65b0\u6587\u4ef6<\/strong><\/h2>\n<p>file_path = Path(&#39;C:\/new_file.txt&#39;)<\/p>\n<p>file_path.write_text(&#39;Hello, C Drive!&#39;)<\/p>\n<h2><strong>\u9a8c\u8bc1\u6587\u4ef6\u662f\u5426\u521b\u5efa<\/strong><\/h2>\n<p>if file_path.exists():<\/p>\n<p>    print(&#39;File created successfully.&#39;)<\/p>\n<h2><strong>\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9<\/strong><\/h2>\n<p>content = file_path.read_text()<\/p>\n<p>print(f&#39;File Content: {content}&#39;)<\/p>\n<h2><strong>\u5220\u9664\u65b0\u521b\u5efa\u7684\u6587\u4ef6<\/strong><\/h2>\n<p>file_path.unlink()<\/p>\n<h2><strong>\u9a8c\u8bc1\u6587\u4ef6\u662f\u5426\u5220\u9664<\/strong><\/h2>\n<p>if not file_path.exists():<\/p>\n<p>    print(&#39;File deleted successfully.&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>write_text<\/code>\u65b9\u6cd5\u521b\u5efa\u5e76\u5199\u5165\u6587\u4ef6\u5185\u5bb9\uff0c\u4f7f\u7528<code>read_text<\/code>\u65b9\u6cd5\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\uff0c\u4f7f\u7528<code>unlink<\/code>\u65b9\u6cd5\u5220\u9664\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5904\u7406\u6587\u4ef6\u548c\u76ee\u5f55\u7684\u6743\u9650<\/h3>\n<\/p>\n<p><p>\u5728\u8bbf\u95eeC\u76d8\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u6587\u4ef6\u6216\u76ee\u5f55\u7684\u6743\u9650\u95ee\u9898\u3002\u4e3a\u4e86\u5904\u7406\u8fd9\u4e9b\u95ee\u9898\uff0c\u53ef\u4ee5\u4f7f\u7528<code>os<\/code>\u5e93\u4e2d\u7684<code>chmod<\/code>\u65b9\u6cd5\u6216<code>pathlib<\/code>\u5e93\u4e2d\u7684<code>chmod<\/code>\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528 os.chmod \u65b9\u6cd5\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u8bbe\u7f6e\u6587\u4ef6\u6743\u9650\u4e3a\u53ef\u8bfb\u5199<\/strong><\/h2>\n<p>file_path = &#39;C:\\\\new_file.txt&#39;<\/p>\n<p>os.chmod(file_path, 0o666)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>0o666<\/code>\u8868\u793a\u8bbe\u7f6e\u6587\u4ef6\u4e3a\u53ef\u8bfb\u5199\u6743\u9650\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528 Path.chmod \u65b9\u6cd5\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<h2><strong>\u8bbe\u7f6e\u6587\u4ef6\u6743\u9650\u4e3a\u53ef\u8bfb\u5199<\/strong><\/h2>\n<p>file_path = Path(&#39;C:\/new_file.txt&#39;)<\/p>\n<p>file_path.chmod(0o666)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>chmod<\/code>\u65b9\u6cd5\u7528\u4e8e\u8bbe\u7f6e\u6587\u4ef6\u6743\u9650\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u5904\u7406\u957f\u8def\u5f84\u95ee\u9898<\/h3>\n<\/p>\n<p><p>\u5728Windows\u7cfb\u7edf\u4e2d\uff0c\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u6587\u4ef6\u8def\u5f84\u7684\u957f\u5ea6\u9650\u5236\u4e3a260\u4e2a\u5b57\u7b26\u3002\u4e3a\u4e86\u5904\u7406\u957f\u8def\u5f84\u95ee\u9898\uff0c\u53ef\u4ee5\u5728Python\u4e2d\u4f7f\u7528\u957f\u8def\u5f84\u524d\u7f00\uff08<code>\\\\?<\/code>\uff09\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528\u957f\u8def\u5f84\u524d\u7f00\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u957f\u8def\u5f84\u524d\u7f00<\/strong><\/h2>\n<p>long_path = &#39;\\\\\\\\?\\\\C:\\\\path_to_your_file&#39;<\/p>\n<h2><strong>\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9<\/strong><\/h2>\n<p>with open(long_path, &#39;r&#39;) as file:<\/p>\n<p>    content = file.read()<\/p>\n<p>    print(content)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u957f\u8def\u5f84\u524d\u7f00\u6765\u5904\u7406\u6587\u4ef6\u8def\u5f84\u957f\u5ea6\u8d85\u8fc7260\u4e2a\u5b57\u7b26\u7684\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u5904\u7406\u4e0d\u540c\u6587\u4ef6\u7cfb\u7edf\u7684\u8def\u5f84\u95ee\u9898<\/h3>\n<\/p>\n<p><p>\u5728\u4e0d\u540c\u7684\u64cd\u4f5c\u7cfb\u7edf\u4e2d\uff0c\u6587\u4ef6\u8def\u5f84\u7684\u8868\u793a\u65b9\u5f0f\u53ef\u80fd\u4e0d\u540c\u3002\u4e3a\u4e86\u7f16\u5199\u8de8\u5e73\u53f0\u7684\u4ee3\u7801\uff0c\u53ef\u4ee5\u4f7f\u7528<code>os.path<\/code>\u6a21\u5757\u4e2d\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528 os.path.join \u65b9\u6cd5\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u8de8\u5e73\u53f0\u7684\u6587\u4ef6\u8def\u5f84\u62fc\u63a5<\/strong><\/h2>\n<p>file_path = os.path.join(&#39;C:&#39;, &#39;path&#39;, &#39;to&#39;, &#39;your&#39;, &#39;file.txt&#39;)<\/p>\n<p>print(file_path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>os.path.join<\/code>\u65b9\u6cd5\u7528\u4e8e\u8de8\u5e73\u53f0\u5730\u62fc\u63a5\u6587\u4ef6\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528 Path \u5bf9\u8c61\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<h2><strong>\u8de8\u5e73\u53f0\u7684\u6587\u4ef6\u8def\u5f84\u62fc\u63a5<\/strong><\/h2>\n<p>file_path = Path(&#39;C:\/path\/to\/your\/file.txt&#39;)<\/p>\n<p>print(file_path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>Path<\/code>\u5bf9\u8c61\u63d0\u4f9b\u4e86\u4e00\u79cd\u66f4\u76f4\u89c2\u7684\u65b9\u6cd5\u6765\u5904\u7406\u6587\u4ef6\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u4f7f\u7528\u73af\u5883\u53d8\u91cf\u8bbf\u95eeC\u76d8<\/h3>\n<\/p>\n<p><p>\u5728Windows\u7cfb\u7edf\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u73af\u5883\u53d8\u91cf\u8bbf\u95eeC\u76d8\u4e2d\u7684\u6587\u4ef6\u548c\u76ee\u5f55\u3002\u4f8b\u5982\uff0c<code>%SystemRoot%<\/code>\u73af\u5883\u53d8\u91cf\u8868\u793aWindows\u76ee\u5f55\u3002<\/p>\n<\/p>\n<p><p><strong>\u8bbf\u95ee\u73af\u5883\u53d8\u91cf\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u83b7\u53d6\u73af\u5883\u53d8\u91cf<\/strong><\/h2>\n<p>system_root = os.getenv(&#39;SystemRoot&#39;)<\/p>\n<p>print(system_root)<\/p>\n<h2><strong>\u5217\u51fa\u73af\u5883\u53d8\u91cf\u76ee\u5f55\u4e2d\u7684\u6587\u4ef6\u548c\u76ee\u5f55<\/strong><\/h2>\n<p>files_in_system_root = os.listdir(system_root)<\/p>\n<p>print(files_in_system_root)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>os.getenv<\/code>\u65b9\u6cd5\u7528\u4e8e\u83b7\u53d6\u73af\u5883\u53d8\u91cf\u7684\u503c\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528 Path \u5bf9\u8c61\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<p>import os<\/p>\n<h2><strong>\u83b7\u53d6\u73af\u5883\u53d8\u91cf<\/strong><\/h2>\n<p>system_root = os.getenv(&#39;SystemRoot&#39;)<\/p>\n<h2><strong>\u521b\u5efaPath\u5bf9\u8c61<\/strong><\/h2>\n<p>system_root_path = Path(system_root)<\/p>\n<p>print(system_root_path)<\/p>\n<h2><strong>\u5217\u51fa\u73af\u5883\u53d8\u91cf\u76ee\u5f55\u4e2d\u7684\u6587\u4ef6\u548c\u76ee\u5f55<\/strong><\/h2>\n<p>for item in system_root_path.iterdir():<\/p>\n<p>    print(item)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>Path<\/code>\u5bf9\u8c61\u6765\u5904\u7406\u73af\u5883\u53d8\u91cf\u8868\u793a\u7684\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u5904\u7406\u6587\u4ef6\u548c\u76ee\u5f55\u7684\u5c5e\u6027<\/h3>\n<\/p>\n<p><p>\u5728\u8bbf\u95eeC\u76d8\u65f6\uff0c\u53ef\u80fd\u9700\u8981\u83b7\u53d6\u6587\u4ef6\u548c\u76ee\u5f55\u7684\u5c5e\u6027\u4fe1\u606f\u3002\u53ef\u4ee5\u4f7f\u7528<code>os.stat<\/code>\u65b9\u6cd5\u6216<code>Path.stat<\/code>\u65b9\u6cd5\u83b7\u53d6\u8fd9\u4e9b\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528 os.stat \u65b9\u6cd5\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u83b7\u53d6\u6587\u4ef6\u5c5e\u6027<\/strong><\/h2>\n<p>file_path = &#39;C:\\\\path_to_your_file.txt&#39;<\/p>\n<p>file_stats = os.stat(file_path)<\/p>\n<p>print(file_stats)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>os.stat<\/code>\u65b9\u6cd5\u8fd4\u56de\u6587\u4ef6\u5c5e\u6027\u7684\u8be6\u7ec6\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528 Path.stat \u65b9\u6cd5\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<h2><strong>\u83b7\u53d6\u6587\u4ef6\u5c5e\u6027<\/strong><\/h2>\n<p>file_path = Path(&#39;C:\/path_to_your_file.txt&#39;)<\/p>\n<p>file_stats = file_path.stat()<\/p>\n<p>print(file_stats)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>Path.stat<\/code>\u65b9\u6cd5\u8fd4\u56de\u6587\u4ef6\u5c5e\u6027\u7684\u8be6\u7ec6\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u5904\u7406\u6587\u4ef6\u7cfb\u7edf\u7684\u5f02\u5e38<\/h3>\n<\/p>\n<p><p>\u5728\u8bbf\u95eeC\u76d8\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u6587\u4ef6\u6216\u76ee\u5f55\u4e0d\u5b58\u5728\u3001\u6743\u9650\u4e0d\u8db3\u7b49\u5f02\u5e38\u3002\u4e3a\u4e86\u5904\u7406\u8fd9\u4e9b\u5f02\u5e38\uff0c\u53ef\u4ee5\u4f7f\u7528<code>try-except<\/code>\u8bed\u53e5\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528 try-except \u5904\u7406\u5f02\u5e38\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u5904\u7406\u6587\u4ef6\u4e0d\u5b58\u5728\u5f02\u5e38<\/strong><\/h2>\n<p>try:<\/p>\n<p>    file_path = &#39;C:\\\\path_to_nonexistent_file.txt&#39;<\/p>\n<p>    with open(file_path, &#39;r&#39;) as file:<\/p>\n<p>        content = file.read()<\/p>\n<p>        print(content)<\/p>\n<p>except FileNotFoundError:<\/p>\n<p>    print(&#39;File not found.&#39;)<\/p>\n<h2><strong>\u5904\u7406\u6743\u9650\u4e0d\u8db3\u5f02\u5e38<\/strong><\/h2>\n<p>try:<\/p>\n<p>    file_path = &#39;C:\\\\path_to_protected_file.txt&#39;<\/p>\n<p>    with open(file_path, &#39;r&#39;) as file:<\/p>\n<p>        content = file.read()<\/p>\n<p>        print(content)<\/p>\n<p>except PermissionError:<\/p>\n<p>    print(&#39;Permission denied.&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>try-except<\/code>\u8bed\u53e5\u7528\u4e8e\u6355\u83b7\u5e76\u5904\u7406\u6587\u4ef6\u4e0d\u5b58\u5728\u548c\u6743\u9650\u4e0d\u8db3\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528 Path \u5bf9\u8c61\u548c try-except \u5904\u7406\u5f02\u5e38\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<h2><strong>\u5904\u7406\u6587\u4ef6\u4e0d\u5b58\u5728\u5f02\u5e38<\/strong><\/h2>\n<p>try:<\/p>\n<p>    file_path = Path(&#39;C:\/path_to_nonexistent_file.txt&#39;)<\/p>\n<p>    content = file_path.read_text()<\/p>\n<p>    print(content)<\/p>\n<p>except FileNotFoundError:<\/p>\n<p>    print(&#39;File not found.&#39;)<\/p>\n<h2><strong>\u5904\u7406\u6743\u9650\u4e0d\u8db3\u5f02\u5e38<\/strong><\/h2>\n<p>try:<\/p>\n<p>    file_path = Path(&#39;C:\/path_to_protected_file.txt&#39;)<\/p>\n<p>    content = file_path.read_text()<\/p>\n<p>    print(content)<\/p>\n<p>except PermissionError:<\/p>\n<p>    print(&#39;Permission denied.&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>Path<\/code>\u5bf9\u8c61\u548c<code>try-except<\/code>\u8bed\u53e5\u5904\u7406\u6587\u4ef6\u4e0d\u5b58\u5728\u548c\u6743\u9650\u4e0d\u8db3\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u5185\u5bb9\uff0c\u6211\u4eec\u8be6\u7ec6\u4ecb\u7ecd\u4e86\u5728Python\u4e2d\u8bbf\u95eeC\u76d8\u7684\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528\u7edd\u5bf9\u8def\u5f84\u548c\u76f8\u5bf9\u8def\u5f84\u3001\u4f7f\u7528<code>os<\/code>\u5e93\u548c<code>pathlib<\/code>\u5e93\u3001\u5904\u7406\u6587\u4ef6\u548c\u76ee\u5f55\u7684\u6743\u9650\u3001\u5904\u7406\u957f\u8def\u5f84\u95ee\u9898\u3001\u5904\u7406\u4e0d\u540c\u6587\u4ef6\u7cfb\u7edf\u7684\u8def\u5f84\u95ee\u9898\u3001\u4f7f\u7528\u73af\u5883\u53d8\u91cf\u3001\u83b7\u53d6\u6587\u4ef6\u548c\u76ee\u5f55\u7684\u5c5e\u6027\u3001\u4ee5\u53ca\u5904\u7406\u6587\u4ef6\u7cfb\u7edf\u7684\u5f02\u5e38\u3002\u5e0c\u671b\u8fd9\u4e9b\u5185\u5bb9\u80fd\u591f\u5e2e\u52a9\u4f60\u5728Python\u7f16\u7a0b\u4e2d\u66f4\u597d\u5730\u8bbf\u95ee\u548c\u64cd\u4f5cC\u76d8\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8bfb\u53d6C\u76d8\u4e0a\u7684\u6587\u4ef6\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>open()<\/code>\u51fd\u6570\u6765\u8bfb\u53d6C\u76d8\u4e0a\u7684\u6587\u4ef6\u3002\u53ea\u9700\u63d0\u4f9b\u6587\u4ef6\u7684\u7edd\u5bf9\u8def\u5f84\uff0c\u4f8b\u5982\uff1a<code>C:\\\\Users\\\\YourUsername\\\\Documents\\\\example.txt<\/code>\u3002\u786e\u4fdd\u8def\u5f84\u4e2d\u7684\u53cd\u659c\u6760\u4f7f\u7528\u53cc\u53cd\u659c\u6760\u8868\u793a\uff0c\u6216\u8005\u53ef\u4ee5\u4f7f\u7528\u539f\u59cb\u5b57\u7b26\u4e32\uff0c\u50cf\u8fd9\u6837\uff1a<code>r&#39;C:\\Users\\YourUsername\\Documents\\example.txt&#39;<\/code>\u3002<\/p>\n<p><strong>Python\u53ef\u4ee5\u7528\u6765\u64cd\u4f5cC\u76d8\u4e0a\u7684\u76ee\u5f55\u5417\uff1f<\/strong><br \/>\u5f53\u7136\u53ef\u4ee5\u3002\u4f7f\u7528<code>os<\/code>\u548c<code>shutil<\/code>\u6a21\u5757\uff0c\u60a8\u53ef\u4ee5\u8f7b\u677e\u5730\u5217\u51fa\u3001\u521b\u5efa\u3001\u5220\u9664\u6216\u79fb\u52a8C\u76d8\u4e0a\u7684\u76ee\u5f55\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>os.listdir(&#39;C:\\\\&#39;)<\/code>\u53ef\u4ee5\u5217\u51faC\u76d8\u6839\u76ee\u5f55\u4e0b\u7684\u6240\u6709\u6587\u4ef6\u548c\u6587\u4ef6\u5939\u3002\u8981\u521b\u5efa\u65b0\u76ee\u5f55\uff0c\u53ef\u4ee5\u4f7f\u7528<code>os.mkdir(&#39;C:\\\\new_folder&#39;)<\/code>\u3002<\/p>\n<p><strong>Python\u5982\u4f55\u5904\u7406C\u76d8\u4e0a\u7684\u6587\u4ef6\u6743\u9650\u95ee\u9898\uff1f<\/strong><br \/>\u5728\u8bbf\u95eeC\u76d8\u6587\u4ef6\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u6743\u9650\u95ee\u9898\u3002\u786e\u4fdd\u60a8\u7684Python\u811a\u672c\u4ee5\u7ba1\u7406\u5458\u6743\u9650\u8fd0\u884c\uff0c\u4ee5\u4fbf\u8bbf\u95ee\u53d7\u4fdd\u62a4\u7684\u6587\u4ef6\u548c\u76ee\u5f55\u3002\u5982\u679c\u60a8\u6ca1\u6709\u76f8\u5e94\u7684\u6743\u9650\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528Windows\u7684\u6587\u4ef6\u548c\u6587\u4ef6\u5939\u5171\u4eab\u529f\u80fd\uff0c\u6216\u8005\u8c03\u6574\u6587\u4ef6\u7684\u5b89\u5168\u8bbe\u7f6e\u4ee5\u5141\u8bb8\u8bbf\u95ee\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u8bbf\u95eeC\u76d8\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff0c\u4f7f\u7528\u7edd\u5bf9\u8def\u5f84\u3001\u4f7f\u7528\u76f8\u5bf9\u8def\u5f84\u3001\u4f7f\u7528os\u5e93\u3001\u4f7f\u7528pathlib\u5e93\u3002 [&hellip;]","protected":false},"author":3,"featured_media":1171036,"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\/1171022"}],"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=1171022"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1171022\/revisions"}],"predecessor-version":[{"id":1171039,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1171022\/revisions\/1171039"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1171036"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1171022"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1171022"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1171022"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}