我正在尝试使用popen打开记事本并向其中写入内容,但是我无法理解它。我可以使用以下命令打开记事本:
notepadprocess=subprocess.Popen('notepad.exe')
我正在尝试确定如何使用Python向文本文件中写入任何内容。欢迎任何帮助。
我正在尝试使用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("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执行了这个任务。