Open a text file window using Python [closed]

冷暖自知 提交于 2020-01-13 07:18:33

问题


I am trying to write a text based game in python that uses a lexicon(A list of accepted inputs and their grammatical parts). Say I have a file lexicon.txt that contains a list of accepted terms for each room, such as attack(verb), dragon(noun), and up(direction). I know that I could print the contents of that file to my program with lexicon.read(), but then after the user has played enough that he can't see that part anymore, he might type an unrecognized word. I would rather have the lexicon constantly opened in another window where you could have easy access to it at any time. Is there any way to make my python file import lexicon.txt and open a window of notepad to display the contents?


回答1:


There are many ways to do this.

This opens the file with the default application:

import os
os.startfile(filename)

To open the file explicitly with notepad use this:

import os
os.system("notepad.exe file.txt")


来源:https://stackoverflow.com/questions/26205275/open-a-text-file-window-using-python

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!