Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
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?
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")
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I have been making a program that opens a different file (extension is .ebj) and shows you the 3d object saved inside it with pygame, so kind of like a simple programming language. Currently i have found how to choose what file you want to open using sys.argv but is there a way to make it so you can run the .ebj file and it will automatically run the python code with the file you ran as the parameter
You description is a bit difficult to decipher. I think you need your python script to run a file? That's what I'm assuming after seeing your reference to sys.argv as holding the file you'd like to run.
If I have that correct, then how you go about running that file is going to be dependent on which OS you're operating on. I'll assume windows, but forgive me if I'm off on that.
Here's how I have my windows machine run a software package using the windows default application based on the file ext.
import subprocess
filename = GET_YOUR_FILENAME_HERE
subprocess.call("start " + filename, shell=True)
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have been making a GUI with pyQt5 and I'm at the point where I need to think about how to save the things the user do in my GUI. For example, if he/she checks a box I want it to be saved until next time they start the GUI.
I have been thinking of using a .ini file, but I'm asking if there is another file type I should be using or is there a function/class in pyQt5.
Sry for my bad English plus I'm on my phone typing.
Ty in advanced.
I would use QSettings, Its part of PyQt/Qt and handles location of your settings/config files.
https://doc.qt.io/qtforpython/PySide2/QtCore/QSettings.html
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I have a text file on sd card which I am reading through python. I want to copy contents of this file and save in another sd card, and as I make changes to original file it should reflect run time on the copied version in real time
I went through Copying from one text file to another using Python but this is static implementation(copied file does not change runtime with changes in original file)
My code:
import os
with open("/path/to/file.txt", 'r') as f:
print (f.read())
#f.flush()
file = open("/path/to/another/file.txt", 'w')
while True:
file.write( f)
file.flush()
# file.close()
If you're trying to track any changes in the original file, from any source, then you have a problem with system security. By definition of the file resource, the OS sees them as distinct entities.
The usual way to handle this is with periodic back-ups. If you require real-time response, then leave a small program running that will detect writes to the original file (ala Tripwire security) and make the changes on demand.
In general, this is not a Python solution; rather it's something to code at the OS level.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
community!
I'm writing my own screensaver using Python 2.7 and Tkinter module. Almost done, but stuck with the following problem:
I have a long-long file with, say, .py extension and a lot of code in it, which I want to display line by line into the GUI desktop, using Tkinter module. I managed to do exactly what I want for console using file.readlines(), but I cannot find out how to do that in GUI.
I have used a tk.Text widget and got the file in full, while I want the lines appear one after another, like when you use f.readlines().
Is there any method to make the tk.Text do that?
You can do by making a Text box :
try:
from tkinter import *
except:
from Tkinter import *
root=Tk()
t=Text(root,height=25,width=100,wrap=WORD)
t.pack()
f=open('a','r')
r=f.readlines()
f.close()
time=1000 # in ms
def f():
global r
t.insert(END,r[0])
r=r[1:]
t.after(time,f)
f()
mainloop()
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Ok so I want to make a simple game where it's essential for the game to save. I wish to store all the data in notepad. However how do I pull notepad into python. I have tried saving notepad into the same folder where my .py file is. Then in the program im not sure what to do. Im just very confused on how to do this. I do know how to program in python, but how do I pull data out of notepad into python.
Also I wish to know how would I change the inside of the notepad file. Let's just say it has a couple of variables with certain numbers. I want to change those in game.
Thank you for any answers. :)
You stored your data in text file not in notepad. Notepad is an application to edit and read the data inside the text file.
Suppose you stored tha data in a text file (whose name is file.txt) using notepad. Now you want to read the data inside text file from your python code. You can read it directly as :
Python code :
file_pointer = open("file.txt", "r");
array = file_pointer.readlines()
print(array[0])
print(array[1])
print(array[2])
print(array[3])
file.txt
25
554
51
4147