{"id":928792,"date":"2024-12-26T16:42:51","date_gmt":"2024-12-26T08:42:51","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/928792.html"},"modified":"2024-12-26T16:42:54","modified_gmt":"2024-12-26T08:42:54","slug":"python%e5%a6%82%e4%bd%95%e8%ae%be%e7%bd%aenotepad","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/928792.html","title":{"rendered":"python\u5982\u4f55\u8bbe\u7f6enotepad"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25064322\/7cb5e1e3-737b-4337-8c82-c917a45a92f9.webp\" alt=\"python\u5982\u4f55\u8bbe\u7f6enotepad\" \/><\/p>\n<p><p> <strong>Python \u8bbe\u7f6e Notepad \u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5176\u4e2d\u5305\u62ec\uff1a\u4f7f\u7528 subprocess \u6a21\u5757\u6253\u5f00 Notepad\u3001\u8bbe\u7f6e\u9ed8\u8ba4\u7684\u6587\u672c\u7f16\u8f91\u5668\u4e3a Notepad\u3001\u4f7f\u7528 Python \u811a\u672c\u5199\u5165\u548c\u8bfb\u53d6 Notepad \u6587\u4ef6<\/strong>\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5229\u7528\u8fd9\u4e9b\u65b9\u6cd5\u6765\u6709\u6548\u5730\u4f7f\u7528 Python \u64cd\u4f5c Notepad\uff0c\u5e76\u901a\u8fc7\u793a\u4f8b\u4ee3\u7801\u8fdb\u884c\u8bf4\u660e\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u6df1\u5165\u63a2\u8ba8\u6bcf\u79cd\u65b9\u6cd5\uff0c\u5e76\u63d0\u4f9b\u5b9e\u7528\u7684\u4ee3\u7801\u793a\u4f8b\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u5229\u7528 subprocess \u6a21\u5757\u6253\u5f00 Notepad<\/p>\n<\/p>\n<p><p>Python \u7684 subprocess \u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u4e2a\u7b80\u5355\u7684\u65b9\u6cd5\u6765\u521b\u5efa\u65b0\u7684\u8fdb\u7a0b\uff0c\u5e76\u4e0e\u5176\u8fdb\u884c\u4ea4\u4e92\u3002\u901a\u8fc7\u8fd9\u4e2a\u6a21\u5757\uff0c\u4f60\u53ef\u4ee5\u5728 Python \u811a\u672c\u4e2d\u8c03\u7528\u5176\u4ed6\u5e94\u7528\u7a0b\u5e8f\uff0c\u5982 Notepad\u3002<\/p>\n<\/p>\n<p><h3>\u4f7f\u7528 subprocess \u6a21\u5757\u6253\u5f00 Notepad<\/h3>\n<\/p>\n<p><p>\u8981\u6253\u5f00 Notepad\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 <code>subprocess.run()<\/code> \u6216 <code>subprocess.Popen()<\/code> \u65b9\u6cd5\u3002\u8fd9\u4e9b\u65b9\u6cd5\u5141\u8bb8\u6211\u4eec\u5728 Python \u811a\u672c\u4e2d\u542f\u52a8 Notepad\uff0c\u5e76\u9009\u62e9\u662f\u5426\u7b49\u5f85\u5176\u5173\u95ed\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import subprocess<\/p>\n<h2><strong>\u4f7f\u7528 subprocess.run \u6253\u5f00 Notepad<\/strong><\/h2>\n<p>subprocess.run([&#39;notepad.exe&#39;])<\/p>\n<h2><strong>\u6216\u8005\u4f7f\u7528 subprocess.Popen \u6253\u5f00 Notepad<\/strong><\/h2>\n<p>subprocess.Popen([&#39;notepad.exe&#39;])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c<code>subprocess.run()<\/code> \u4f1a\u7b49\u5f85 Notepad \u5173\u95ed\u540e\u624d\u4f1a\u7ee7\u7eed\u6267\u884c\u540e\u7eed\u4ee3\u7801\uff0c\u800c <code>subprocess.Popen()<\/code> \u5219\u4e0d\u4f1a\u7b49\u5f85 Notepad \u5173\u95ed\u3002<\/p>\n<\/p>\n<p><h3>\u4f20\u9012\u6587\u4ef6\u5230 Notepad<\/h3>\n<\/p>\n<p><p>\u4f60\u8fd8\u53ef\u4ee5\u5728\u6253\u5f00 Notepad \u7684\u540c\u65f6\u4f20\u9012\u6587\u4ef6\u8def\u5f84\uff0c\u4ee5\u4fbf\u76f4\u63a5\u6253\u5f00\u6307\u5b9a\u7684\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import subprocess<\/p>\n<h2><strong>\u6253\u5f00\u5e76\u8bfb\u53d6\u540d\u4e3a example.txt \u7684\u6587\u4ef6<\/strong><\/h2>\n<p>subprocess.run([&#39;notepad.exe&#39;, &#39;example.txt&#39;])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u4f60\u53ef\u4ee5\u8ba9 Notepad \u76f4\u63a5\u6253\u5f00\u5e76\u663e\u793a <code>example.txt<\/code> \u6587\u4ef6\u7684\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u8bbe\u7f6e Notepad \u4e3a\u9ed8\u8ba4\u6587\u672c\u7f16\u8f91\u5668<\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4f60\u53ef\u80fd\u5e0c\u671b\u5c06 Notepad \u8bbe\u7f6e\u4e3a Python \u811a\u672c\u7684\u9ed8\u8ba4\u6587\u672c\u7f16\u8f91\u5668\u3002\u8fd9\u53ef\u4ee5\u901a\u8fc7\u4fee\u6539\u7cfb\u7edf\u8bbe\u7f6e\u6216\u5728 Python \u811a\u672c\u4e2d\u6307\u5b9a\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><h3>\u4fee\u6539\u7cfb\u7edf\u8bbe\u7f6e<\/h3>\n<\/p>\n<p><p>\u8981\u5c06 Notepad \u8bbe\u7f6e\u4e3a\u9ed8\u8ba4\u6587\u672c\u7f16\u8f91\u5668\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u64cd\u4f5c\u7cfb\u7edf\u7684\u8bbe\u7f6e\u6765\u5b9e\u73b0\u3002\u4f8b\u5982\uff0c\u5728 Windows \u7cfb\u7edf\u4e2d\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u201c\u9ed8\u8ba4\u5e94\u7528\u8bbe\u7f6e\u201d\u6765\u66f4\u6539\u9ed8\u8ba4\u7684\u6587\u672c\u7f16\u8f91\u5668\u3002<\/p>\n<\/p>\n<p><h3>\u5728 Python \u811a\u672c\u4e2d\u6307\u5b9a\u9ed8\u8ba4\u7f16\u8f91\u5668<\/h3>\n<\/p>\n<p><p>\u5728 Python \u811a\u672c\u4e2d\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u521b\u5efa\u4e00\u4e2a\u51fd\u6570\u6765\u6307\u5b9a\u9ed8\u8ba4\u7684\u6587\u672c\u7f16\u8f91\u5668\uff0c\u5e76\u5728\u9700\u8981\u65f6\u8c03\u7528\u5b83\u6765\u6253\u5f00\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>def open_with_notepad(file_path):<\/p>\n<p>    os.system(f&#39;notepad.exe {file_path}&#39;)<\/p>\n<h2><strong>\u4f7f\u7528\u8be5\u51fd\u6570\u6253\u5f00\u6587\u4ef6<\/strong><\/h2>\n<p>open_with_notepad(&#39;example.txt&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u8ba9\u4f60\u80fd\u591f\u5728 Python \u811a\u672c\u4e2d\u7075\u6d3b\u5730\u6307\u5b9a\u548c\u4f7f\u7528 Notepad \u4f5c\u4e3a\u9ed8\u8ba4\u7684\u6587\u672c\u7f16\u8f91\u5668\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528 Python \u811a\u672c\u5199\u5165\u548c\u8bfb\u53d6 Notepad \u6587\u4ef6<\/p>\n<\/p>\n<p><p>Python \u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6587\u4ef6\u64cd\u4f5c\u529f\u80fd\uff0c\u53ef\u4ee5\u8ba9\u4f60\u8f7b\u677e\u5730\u5199\u5165\u548c\u8bfb\u53d6\u6587\u4ef6\u3002\u8fd9\u5bf9\u4e8e\u9700\u8981\u5728 Notepad \u4e2d\u67e5\u770b\u548c\u7f16\u8f91\u7684\u6570\u636e\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u5199\u5165\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528 Python \u7684\u5185\u7f6e\u51fd\u6570 <code>open()<\/code>\uff0c\u4f60\u53ef\u4ee5\u8f7b\u677e\u5730\u521b\u5efa\u548c\u5199\u5165\u6587\u672c\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u5e76\u5199\u5165 example.txt \u6587\u4ef6<\/p>\n<p>with open(&#39;example.txt&#39;, &#39;w&#39;) as file:<\/p>\n<p>    file.write(&#39;Hello, this is a test file for Notepad.&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 <code>open()<\/code> \u51fd\u6570\u6253\u5f00\uff08\u6216\u521b\u5efa\uff09\u4e00\u4e2a\u540d\u4e3a <code>example.txt<\/code> \u7684\u6587\u4ef6\uff0c\u5e76\u4f7f\u7528 <code>write()<\/code> \u65b9\u6cd5\u5199\u5165\u6587\u672c\u3002<\/p>\n<\/p>\n<p><h3>\u8bfb\u53d6\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u540c\u6837\u5730\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 <code>open()<\/code> \u51fd\u6570\u8bfb\u53d6\u6587\u672c\u6587\u4ef6\u7684\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8bfb\u53d6 example.txt \u6587\u4ef6\u7684\u5185\u5bb9<\/p>\n<p>with open(&#39;example.txt&#39;, &#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>\u8fd9\u6bb5\u4ee3\u7801\u5c06\u6253\u5f00 <code>example.txt<\/code> \u6587\u4ef6\uff0c\u5e76\u6253\u5370\u5176\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u7efc\u5408\u5e94\u7528\uff1a\u81ea\u52a8\u5316 Notepad \u64cd\u4f5c<\/p>\n<\/p>\n<p><p>\u7ed3\u5408\u4ee5\u4e0a\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a Python \u811a\u672c\uff0c\u81ea\u52a8\u5316 Notepad \u7684\u64cd\u4f5c\u3002\u8fd9\u4e2a\u811a\u672c\u5c06\u521b\u5efa\u4e00\u4e2a\u6587\u672c\u6587\u4ef6\uff0c\u5199\u5165\u4e00\u4e9b\u5185\u5bb9\uff0c\u5e76\u4f7f\u7528 Notepad \u6253\u5f00\u5b83\u3002<\/p>\n<\/p>\n<p><h3>\u81ea\u52a8\u5316\u811a\u672c\u793a\u4f8b<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">import subprocess<\/p>\n<p>def create_and_open_file(file_path, content):<\/p>\n<p>    # \u521b\u5efa\u5e76\u5199\u5165\u6587\u4ef6<\/p>\n<p>    with open(file_path, &#39;w&#39;) as file:<\/p>\n<p>        file.write(content)<\/p>\n<p>    # \u4f7f\u7528 Notepad \u6253\u5f00\u6587\u4ef6<\/p>\n<p>    subprocess.run([&#39;notepad.exe&#39;, file_path])<\/p>\n<h2><strong>\u81ea\u52a8\u5316\u521b\u5efa\u548c\u6253\u5f00\u6587\u4ef6<\/strong><\/h2>\n<p>create_and_open_file(&#39;example.txt&#39;, &#39;This is an automated file created by Python.&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u4f60\u53ef\u4ee5\u5229\u7528 Python \u7684\u5f3a\u5927\u529f\u80fd\u6765\u7b80\u5316\u548c\u81ea\u52a8\u5316 Notepad \u7684\u64cd\u4f5c\uff0c\u63d0\u9ad8\u5de5\u4f5c\u6548\u7387\u3002<\/p>\n<\/p>\n<p><p>\u603b\u7ed3\uff1a<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4f7f\u7528 Python \u7684 subprocess \u6a21\u5757\u548c\u6587\u4ef6\u64cd\u4f5c\u529f\u80fd\uff0c\u4f60\u53ef\u4ee5\u8f7b\u677e\u5730\u5728 Python \u811a\u672c\u4e2d\u8bbe\u7f6e\u548c\u4f7f\u7528 Notepad\u3002\u65e0\u8bba\u662f\u6253\u5f00 Notepad\u3001\u8bbe\u7f6e\u9ed8\u8ba4\u6587\u672c\u7f16\u8f91\u5668\uff0c\u8fd8\u662f\u81ea\u52a8\u5316\u6587\u4ef6\u64cd\u4f5c\uff0cPython \u90fd\u63d0\u4f9b\u4e86\u7075\u6d3b\u800c\u5f3a\u5927\u7684\u5de5\u5177\u6765\u5b9e\u73b0\u8fd9\u4e9b\u76ee\u6807\u3002\u5e0c\u671b\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u4f60\u80fd\u591f\u66f4\u597d\u5730\u7406\u89e3\u548c\u5e94\u7528\u8fd9\u4e9b\u6280\u672f\uff0c\u5728\u5de5\u4f5c\u4e2d\u53d6\u5f97\u66f4\u5927\u7684\u6548\u7387\u63d0\u5347\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u6253\u5f00Notepad\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528Python\u7684<code>os<\/code>\u6a21\u5757\u6216<code>subprocess<\/code>\u6a21\u5757\u6765\u6253\u5f00Notepad\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import os\nos.system(&#39;notepad.exe&#39;)\n<\/code><\/pre>\n<p>\u6216\u8005\u4f7f\u7528<code>subprocess<\/code>\u6a21\u5757\uff1a  <\/p>\n<pre><code class=\"language-python\">import subprocess\nsubprocess.run([&#39;notepad.exe&#39;])\n<\/code><\/pre>\n<p>\u8fd9\u4e24\u79cd\u65b9\u5f0f\u90fd\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u6253\u5f00Notepad\u7684\u529f\u80fd\u3002<\/p>\n<p><strong>Python\u662f\u5426\u53ef\u4ee5\u901a\u8fc7Notepad\u7f16\u8f91\u6587\u4ef6\uff1f<\/strong><br \/>\u662f\u7684\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528Python\u521b\u5efa\u6216\u7f16\u8f91\u6587\u672c\u6587\u4ef6\uff0c\u5e76\u901a\u8fc7Notepad\u67e5\u770b\u6216\u4fee\u6539\u8fd9\u4e9b\u6587\u4ef6\u3002\u4f7f\u7528Python\u7684\u6587\u4ef6\u64cd\u4f5c\u529f\u80fd\uff0c\u4f8b\u5982<code>open()<\/code>\u548c<code>write()<\/code>\uff0c\u53ef\u4ee5\u751f\u6210\u6587\u672c\u6587\u4ef6\uff0c\u7136\u540e\u901a\u8fc7Notepad\u6253\u5f00\u8fdb\u884c\u7f16\u8f91\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">with open(&#39;example.txt&#39;, &#39;w&#39;) as f:\n    f.write(&#39;Hello, World!&#39;)\n<\/code><\/pre>\n<p>\u63a5\u7740\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e0a\u9762\u63d0\u5230\u7684\u65b9\u6cd5\u6253\u5f00Notepad\u6765\u67e5\u770b<code>example.txt<\/code>\u3002<\/p>\n<p><strong>\u5982\u4f55\u901a\u8fc7Python\u811a\u672c\u81ea\u52a8\u4fdd\u5b58Notepad\u4e2d\u7684\u5185\u5bb9\uff1f<\/strong><br \/>Python\u672c\u8eab\u5e76\u4e0d\u80fd\u76f4\u63a5\u63a7\u5236Notepad\u7684\u5185\u5bb9\u4fdd\u5b58\uff0c\u4f46\u53ef\u4ee5\u7f16\u5199\u811a\u672c\u6765\u5b9a\u671f\u4fdd\u5b58\u6587\u4ef6\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528<code>pyautogui<\/code>\u5e93\u6a21\u62df\u952e\u76d8\u64cd\u4f5c\uff0c\u5728Notepad\u4e2d\u4f7f\u7528\u5feb\u6377\u952e\uff08\u5982Ctrl+S\uff09\u8fdb\u884c\u4fdd\u5b58\u3002\u5b89\u88c5<code>pyautogui<\/code>\u540e\uff0c\u53ef\u4ee5\u7f16\u5199\u5982\u4e0b\u4ee3\u7801\uff1a  <\/p>\n<pre><code class=\"language-python\">import pyautogui\nimport time\n\ntime.sleep(5)  # \u7ed9\u4f60\u65f6\u95f4\u53bb\u6253\u5f00Notepad\npyautogui.hotkey(&#39;ctrl&#39;, &#39;s&#39;)  # \u6a21\u62df\u6309\u4e0b Ctrl + S\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u4f1a\u57285\u79d2\u540e\u81ea\u52a8\u6267\u884c\u4fdd\u5b58\u64cd\u4f5c\u3002\u786e\u4fdd\u5728\u8fd0\u884c\u811a\u672c\u524d\u5df2\u7ecf\u6253\u5f00Notepad\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python \u8bbe\u7f6e Notepad \u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5176\u4e2d\u5305\u62ec\uff1a\u4f7f\u7528 subprocess \u6a21\u5757\u6253\u5f00 Notepa [&hellip;]","protected":false},"author":3,"featured_media":928799,"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\/928792"}],"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=928792"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/928792\/revisions"}],"predecessor-version":[{"id":928800,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/928792\/revisions\/928800"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/928799"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=928792"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=928792"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=928792"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}