{"id":1183446,"date":"2025-01-15T19:14:44","date_gmt":"2025-01-15T11:14:44","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1183446.html"},"modified":"2025-01-15T19:14:46","modified_gmt":"2025-01-15T11:14:46","slug":"python%e5%a6%82%e4%bd%95%e8%b0%83%e7%94%a8%e6%a1%8c%e9%9d%a2%e7%9a%84%e6%96%87%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1183446.html","title":{"rendered":"python\u5982\u4f55\u8c03\u7528\u684c\u9762\u7684\u6587\u4ef6"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25133221\/66260bea-8246-405f-8670-e18443d9b065.webp\" alt=\"python\u5982\u4f55\u8c03\u7528\u684c\u9762\u7684\u6587\u4ef6\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u591a\u4e2a\u5e93\u548c\u65b9\u6cd5\u6765\u8c03\u7528\u684c\u9762\u7684\u6587\u4ef6\uff0c\u5982os\u3001pathlib\u3001shutil\u7b49\u3002\u9996\u5148\uff0c\u5b9a\u4f4d\u684c\u9762\u8def\u5f84\u975e\u5e38\u91cd\u8981\uff0c\u5176\u6b21\uff0c\u6253\u5f00\u6587\u4ef6\u6216\u8005\u5bf9\u6587\u4ef6\u8fdb\u884c\u64cd\u4f5c\u662f\u5173\u952e\u6b65\u9aa4\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u83b7\u53d6\u684c\u9762\u7684\u8def\u5f84\u3002\u53ef\u4ee5\u4f7f\u7528<code>os<\/code>\u6a21\u5757\u6216\u8005<code>pathlib<\/code>\u6a21\u5757\u6765\u83b7\u53d6\u684c\u9762\u8def\u5f84\u3002<strong>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>os.path.expanduser<\/code>\u6765\u83b7\u53d6\u7528\u6237\u7684\u4e3b\u76ee\u5f55\u8def\u5f84\uff0c\u7136\u540e\u62fc\u63a5\u684c\u9762\u8def\u5f84<\/strong>\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>os<\/code>\u6a21\u5757\u4e2d\u7684\u65b9\u6cd5\u6765\u64cd\u4f5c\u6587\u4ef6\uff0c\u6bd4\u5982\u6253\u5f00\u3001\u8bfb\u53d6\u3001\u5199\u5165\u7b49\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u83b7\u53d6\u684c\u9762\u8def\u5f84<\/h3>\n<\/p>\n<p><p><strong>\u4f7f\u7528<code>os<\/code>\u6a21\u5757\u83b7\u53d6\u684c\u9762\u8def\u5f84\u662f\u5e38\u89c1\u7684\u65b9\u6cd5\u4e4b\u4e00<\/strong>\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7<code>os.path.expanduser<\/code>\u83b7\u53d6\u7528\u6237\u4e3b\u76ee\u5f55\uff0c\u7136\u540e\u62fc\u63a5\u684c\u9762\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>def get_desktop_path():<\/p>\n<p>    desktop_path = os.path.join(os.path.expanduser(&quot;~&quot;), &quot;Desktop&quot;)<\/p>\n<p>    return desktop_path<\/p>\n<p>desktop_path = get_desktop_path()<\/p>\n<p>print(f&quot;Desktop Path: {desktop_path}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u53e6\u4e00\u79cd\u65b9\u6cd5\u662f\u4f7f\u7528<code>pathlib<\/code>\u6a21\u5757\uff0c\u5b83\u63d0\u4f9b\u4e86\u66f4\u73b0\u4ee3\u548c\u9762\u5411\u5bf9\u8c61\u7684\u65b9\u5f0f\u6765\u64cd\u4f5c\u6587\u4ef6\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<p>def get_desktop_path():<\/p>\n<p>    desktop_path = Path.home() \/ &#39;Desktop&#39;<\/p>\n<p>    return desktop_path<\/p>\n<p>desktop_path = get_desktop_path()<\/p>\n<p>print(f&quot;Desktop Path: {desktop_path}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u64cd\u4f5c\u684c\u9762\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u83b7\u53d6\u4e86\u684c\u9762\u7684\u8def\u5f84\u4e4b\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u5bf9\u684c\u9762\u4e0a\u7684\u6587\u4ef6\u8fdb\u884c\u5404\u79cd\u64cd\u4f5c\uff0c\u6bd4\u5982\u6253\u5f00\u6587\u4ef6\u3001\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u3001\u5199\u5165\u6587\u4ef6\u7b49\u7b49\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u68c0\u67e5\u6587\u4ef6\u662f\u5426\u5b58\u5728<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>os.path.exists<\/code>\u6216\u8005<code>pathlib.Path.exists<\/code>\u6765\u68c0\u67e5\u6587\u4ef6\u662f\u5426\u5b58\u5728\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">file_name = &quot;example.txt&quot;<\/p>\n<p>file_path = os.path.join(desktop_path, file_name)<\/p>\n<p>if os.path.exists(file_path):<\/p>\n<p>    print(f&quot;{file_name} exists on the desktop.&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(f&quot;{file_name} does not exist on the desktop.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><pre><code class=\"language-python\">file_path = desktop_path \/ &quot;example.txt&quot;<\/p>\n<p>if file_path.exists():<\/p>\n<p>    print(f&quot;{file_path.name} exists on the desktop.&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(f&quot;{file_path.name} does not exist on the desktop.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>open<\/code>\u51fd\u6570\u6765\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">if os.path.exists(file_path):<\/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><\/code><\/pre>\n<\/p>\n<p><pre><code class=\"language-python\">if file_path.exists():<\/p>\n<p>    with file_path.open(&#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><h4>3\u3001\u5199\u5165\u6587\u4ef6\u5185\u5bb9<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>open<\/code>\u51fd\u6570\u5e76\u6307\u5b9a\u5199\u5165\u6a21\u5f0f\u6765\u5199\u5165\u6587\u4ef6\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">file_path = os.path.join(desktop_path, &quot;example.txt&quot;)<\/p>\n<p>with open(file_path, &#39;w&#39;) as file:<\/p>\n<p>    file.write(&quot;Hello, Desktop!&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><pre><code class=\"language-python\">file_path = desktop_path \/ &quot;example.txt&quot;<\/p>\n<p>with file_path.open(&#39;w&#39;) as file:<\/p>\n<p>    file.write(&quot;Hello, Desktop!&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u79fb\u52a8\u3001\u590d\u5236\u548c\u5220\u9664\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u6211\u4eec\u8fd8\u53ef\u4ee5\u4f7f\u7528<code>shutil<\/code>\u6a21\u5757\u6765\u79fb\u52a8\u3001\u590d\u5236\u548c\u5220\u9664\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u590d\u5236\u6587\u4ef6<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import shutil<\/p>\n<p>source_file = desktop_path \/ &quot;example.txt&quot;<\/p>\n<p>destination_file = desktop_path \/ &quot;example_copy.txt&quot;<\/p>\n<p>shutil.copy(source_file, destination_file)<\/p>\n<p>print(f&quot;Copied {source_file.name} to {destination_file.name}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u79fb\u52a8\u6587\u4ef6<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">destination_path = desktop_path \/ &quot;example_moved.txt&quot;<\/p>\n<p>shutil.move(source_file, destination_path)<\/p>\n<p>print(f&quot;Moved {source_file.name} to {destination_path.name}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u5220\u9664\u6587\u4ef6<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">os.remove(destination_file)<\/p>\n<p>print(f&quot;Deleted {destination_file.name}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><pre><code class=\"language-python\">destination_file.unlink()<\/p>\n<p>print(f&quot;Deleted {destination_file.name}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u5217\u51fa\u684c\u9762\u6240\u6709\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>os.listdir<\/code>\u6216\u8005<code>pathlib.Path.iterdir<\/code>\u6765\u5217\u51fa\u684c\u9762\u4e0a\u7684\u6240\u6709\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">files = os.listdir(desktop_path)<\/p>\n<p>print(&quot;Files on Desktop:&quot;)<\/p>\n<p>for file in files:<\/p>\n<p>    print(file)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><pre><code class=\"language-python\">files = desktop_path.iterdir()<\/p>\n<p>print(&quot;Files on Desktop:&quot;)<\/p>\n<p>for file in files:<\/p>\n<p>    print(file.name)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u793a\u4f8b\u4ee3\u7801\u6c47\u603b<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5b8c\u6574\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u83b7\u53d6\u684c\u9762\u8def\u5f84\u5e76\u8fdb\u884c\u5404\u79cd\u6587\u4ef6\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>from pathlib import Path<\/p>\n<p>import shutil<\/p>\n<p>def get_desktop_path():<\/p>\n<p>    return Path.home() \/ &#39;Desktop&#39;<\/p>\n<p>desktop_path = get_desktop_path()<\/p>\n<p>print(f&quot;Desktop Path: {desktop_path}&quot;)<\/p>\n<h2><strong>Check if file exists<\/strong><\/h2>\n<p>file_path = desktop_path \/ &quot;example.txt&quot;<\/p>\n<p>if file_path.exists():<\/p>\n<p>    print(f&quot;{file_path.name} exists on the desktop.&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(f&quot;{file_path.name} does not exist on the desktop.&quot;)<\/p>\n<h2><strong>Read file content<\/strong><\/h2>\n<p>if file_path.exists():<\/p>\n<p>    with file_path.open(&#39;r&#39;) as file:<\/p>\n<p>        content = file.read()<\/p>\n<p>        print(content)<\/p>\n<h2><strong>Write file content<\/strong><\/h2>\n<p>with file_path.open(&#39;w&#39;) as file:<\/p>\n<p>    file.write(&quot;Hello, Desktop!&quot;)<\/p>\n<h2><strong>Copy file<\/strong><\/h2>\n<p>source_file = file_path<\/p>\n<p>destination_file = desktop_path \/ &quot;example_copy.txt&quot;<\/p>\n<p>shutil.copy(source_file, destination_file)<\/p>\n<p>print(f&quot;Copied {source_file.name} to {destination_file.name}&quot;)<\/p>\n<h2><strong>Move file<\/strong><\/h2>\n<p>destination_path = desktop_path \/ &quot;example_moved.txt&quot;<\/p>\n<p>shutil.move(source_file, destination_path)<\/p>\n<p>print(f&quot;Moved {source_file.name} to {destination_path.name}&quot;)<\/p>\n<h2><strong>Delete file<\/strong><\/h2>\n<p>destination_file.unlink()<\/p>\n<p>print(f&quot;Deleted {destination_file.name}&quot;)<\/p>\n<h2><strong>List files on Desktop<\/strong><\/h2>\n<p>files = desktop_path.iterdir()<\/p>\n<p>print(&quot;Files on Desktop:&quot;)<\/p>\n<p>for file in files:<\/p>\n<p>    print(file.name)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\u548c\u4ee3\u7801\uff0c\u60a8\u53ef\u4ee5\u8f7b\u677e\u5730\u5728Python\u4e2d\u8c03\u7528\u684c\u9762\u7684\u6587\u4ef6\u5e76\u8fdb\u884c\u5404\u79cd\u64cd\u4f5c\u3002\u5e0c\u671b\u8fd9\u4e9b\u5185\u5bb9\u5bf9\u60a8\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8bbf\u95ee\u684c\u9762\u6587\u4ef6\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>os<\/code>\u5e93\u548c<code>pathlib<\/code>\u5e93\u6765\u8bbf\u95ee\u684c\u9762\u6587\u4ef6\u3002\u901a\u8fc7\u83b7\u53d6\u7528\u6237\u7684\u684c\u9762\u8def\u5f84\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u8bfb\u53d6\u6216\u64cd\u4f5c\u6587\u4ef6\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>os.path.expanduser(&quot;~\/Desktop\/filename.txt&quot;)<\/code>\u53ef\u4ee5\u83b7\u53d6\u684c\u9762\u4e0a\u67d0\u4e2a\u6587\u4ef6\u7684\u5b8c\u6574\u8def\u5f84\u3002<\/p>\n<p><strong>Python\u80fd\u5426\u6253\u5f00\u684c\u9762\u4e0a\u7684\u6587\u4ef6\uff1f<\/strong><br \/>\u662f\u7684\uff0cPython\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u6253\u5f00\u684c\u9762\u4e0a\u7684\u6587\u4ef6\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>os.startfile()<\/code>\u65b9\u6cd5\u6765\u76f4\u63a5\u6253\u5f00\u684c\u9762\u4e0a\u7684\u6587\u4ef6\uff0c\u6216\u8005\u4f7f\u7528<code>subprocess<\/code>\u6a21\u5757\u6765\u8c03\u7528\u5176\u4ed6\u5e94\u7528\u7a0b\u5e8f\u6253\u5f00\u8be5\u6587\u4ef6\u3002\u8fd9\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u5e2e\u52a9\u60a8\u5728\u7f16\u7a0b\u4e2d\u5b9e\u73b0\u5bf9\u6587\u4ef6\u7684\u5feb\u901f\u8bbf\u95ee\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u5217\u51fa\u684c\u9762\u4e0a\u7684\u6240\u6709\u6587\u4ef6\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528<code>os.listdir()<\/code>\u51fd\u6570\u6216<code>pathlib.Path<\/code>\u7c7b\u6765\u5217\u51fa\u684c\u9762\u4e0a\u7684\u6240\u6709\u6587\u4ef6\u3002\u901a\u8fc7\u83b7\u53d6\u684c\u9762\u7684\u8def\u5f84\u5e76\u4f20\u9012\u7ed9\u8fd9\u4e9b\u51fd\u6570\uff0c\u60a8\u53ef\u4ee5\u83b7\u53d6\u684c\u9762\u4e0a\u6240\u6709\u6587\u4ef6\u548c\u6587\u4ef6\u5939\u7684\u5217\u8868\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import os\ndesktop_path = os.path.join(os.path.expanduser(&quot;~&quot;), &quot;Desktop&quot;)\nfiles = os.listdir(desktop_path)\nprint(files)\n<\/code><\/pre>\n<p><strong>Python\u5982\u4f55\u5904\u7406\u684c\u9762\u6587\u4ef6\u7684\u8def\u5f84\u95ee\u9898\uff1f<\/strong><br \/>\u5904\u7406\u6587\u4ef6\u8def\u5f84\u65f6\uff0c\u5e94\u6ce8\u610f\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u7684\u8def\u5f84\u5206\u9694\u7b26\u95ee\u9898\u3002\u4f7f\u7528<code>os.path.join()<\/code>\u53ef\u4ee5\u786e\u4fdd\u8def\u5f84\u5728\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u4e0a\u7684\u517c\u5bb9\u6027\u3002\u6b64\u5916\uff0c<code>pathlib<\/code>\u5e93\u63d0\u4f9b\u4e86\u9762\u5411\u5bf9\u8c61\u7684\u8def\u5f84\u5904\u7406\u65b9\u5f0f\uff0c\u4f7f\u5f97\u8def\u5f84\u64cd\u4f5c\u66f4\u52a0\u76f4\u89c2\u548c\u65b9\u4fbf\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u591a\u4e2a\u5e93\u548c\u65b9\u6cd5\u6765\u8c03\u7528\u684c\u9762\u7684\u6587\u4ef6\uff0c\u5982os\u3001pathlib\u3001shutil\u7b49\u3002\u9996\u5148\uff0c\u5b9a\u4f4d [&hellip;]","protected":false},"author":3,"featured_media":1183451,"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\/1183446"}],"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=1183446"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1183446\/revisions"}],"predecessor-version":[{"id":1183454,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1183446\/revisions\/1183454"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1183451"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1183446"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1183446"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1183446"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}