Python 3.x python-自动启动程序中创建的文本文件
我做了搜索,但找不到任何帮助,如果我的问题重复,我道歉 我正在用python 3.6在windows环境中编写代码。在我的代码中,我打开了一个文本文件,写入数据并关闭文件Python 3.x python-自动启动程序中创建的文本文件,python-3.x,Python 3.x,我做了搜索,但找不到任何帮助,如果我的问题重复,我道歉 我正在用python 3.6在windows环境中编写代码。在我的代码中,我打开了一个文本文件,写入数据并关闭文件 self.fileName = 'file path' self.log_file = open(self.fileName, 'w') self.log_file.write('Write results') self.lof_file.close() 我希望在python保存文件后自动启动该文件,而不是用户转到文件路径并
self.fileName = 'file path'
self.log_file = open(self.fileName, 'w')
self.log_file.write('Write results')
self.lof_file.close()
我希望在python保存文件后自动启动该文件,而不是用户转到文件路径并单击打开它
我该怎么做?请帮忙
编辑:
命令工作正常,但它是用默认程序Notepad打开的,如何用特定程序打开文件,例如Notepad++如果您知道命令行的方法,您可以按如下方式使用
os
模块:
import os
self.file = 'file path'
self.log_file = open(self.fileName, 'w')
self.log_file.write('Write results')
self.lof_file.close()
os.system('gedit <file_path>') # for ubuntu, gedit is generally present
查看此答案了解更多详细信息:您所说的“启动文件”是什么意思?在文本编辑器中?是的,我创建的文件为*.txt,要用记事本或记事本++打开它。这并没有回答我的问题,你的文件应该如何启动?在哪个文本编辑器中?这取决于操作系统。我使用的是Windows平台,而不是默认程序。我想用另一个程序打开它。请看一下模块,尤其是
子流程。Popen
。对不起,请您解释一下,我使用的是windows@mortuzahasan更新了我的答案。
import os
self.file = 'file path'
self.log_file = open(self.fileName, 'w')
self.log_file.write('Write results')
self.lof_file.close()
os.system('gedit <file_path>') # for ubuntu, gedit is generally present
import os
os.startfile('C:\\Users\\RandomUser\\Documents\\test.txt')