python-3.x - python - 自动启动在程序中创建的文本文件
问题描述
我进行了搜索,但找不到任何帮助,如果我的问题重复,我深表歉意。
我正在使用 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 保存文件后自动启动文件。
我怎么做?请帮忙
编辑:
os.startfile(filepath=self.fileName)
命令工作正常,但它使用默认程序记事本打开,如何使用特定程序打开文件,例如记事本++
解决方案
如果你知道命令行的方式,你可以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
对于 Windows,您可以使用:
import os
os.startfile('C:\\Users\\RandomUser\\Documents\\test.txt')
检查此答案以获取更多详细信息:https ://stackoverflow.com/a/15055133/9332801
推荐阅读
- java - 如何使用 find() 方法从字符串中提取所有数字?
- javascript - 如何使计算按钮与已单击的特定功能一起使用
- c# - 如何保存 AndroidManifest.xml 文件 Unity?
- python - 在 Pandas 的两个单独列中返回第一个值,然后是所有其他值
- html - 按路径加载静态图片 (Vue.js)
- javascript - 我可以用这些数据在 d3 中可视化二叉树吗
- sql - 在 Microsoft SQL 中,如何每个 ID 仅返回 1 行,多列结果
- jquery - 在保持键盘的同时专注于下一个输入
- vb.net - 如何获取 TraceSource 列表或按名称检索 TraceSource?
- nuget-package - 如何在 csproj 中指定提交和分支以创建 nuget 包