记事本的Python子进程
我试着用popen打开记事本,在里面写些东西。我的头脑绕不过去。我可以使用以下命令打开记事本: notepadprocess=subprocess.Popen('notepad.exe')记事本的Python子进程,python,subprocess,popen,notepad,Python,Subprocess,Popen,Notepad,我试着用popen打开记事本,在里面写些东西。我的头脑绕不过去。我可以使用以下命令打开记事本: notepadprocess=subprocess.Popen('notepad.exe') 我试图确定如何使用python在文本文件中编写任何内容。非常感谢您的帮助。您可以先将内容写入txt文件(例如foo.txt),然后用记事本打开: import os f = open('foo.txt','w') f.write('Hello world!') f.close() os.system("no
我试图确定如何使用python在文本文件中编写任何内容。非常感谢您的帮助。您可以先将内容写入txt文件(例如foo.txt),然后用记事本打开:
import os
f = open('foo.txt','w')
f.write('Hello world!')
f.close()
os.system("notepad.exe foo.txt")
您可能会混淆(文本)文件的概念和操作它们的过程 记事本是一个程序,您可以创建一个过程。另一方面,文件只是硬盘上的一种结构 从编程的角度来看,记事本不编辑文件。它:
- 将文件读入计算机内存
- 修改该内存的内容
- 将该内存写回一个文件(可以类似地命名,也可以其他方式命名,这称为“另存为”操作)
my_file= "myfile.txt" #the name/path of the file
with open(file, "rb") as f: #open the file for reading
content= f.read() #read the file into memory
content+= "mytext" #change the memory
with open(file, "wb") as f: #open the file for writing
f.write( content ) #write the memory into the file
从Alex K的评论中找到了确切的解决方案。我使用pywinauto执行此任务 为什么你不能直接写一个文件呢?我正在尝试自动化:)你不必打开记事本。您可以从python写入文件。(UIAutomation)相关:我可以这样做,但我正试图打开一个带有一些文本(使用python)的记事本。@BashNoob:如果运行Tim Osadchiy的代码会发生什么?我希望它能用
Hello world打开记事本代码>其中的文本。你看到了什么?@J.F.Sebastian我可以这样做,但我发现使用pywinauto更有效。谢谢你的澄清。我正试图解释第三部分。这就是我正在尝试做的事情——打开记事本并写一些东西,就像打开CMD提示符和使用POPON发送命令一样。它也可能适用于您的情况。