#๐ read() works only one time in python
26 messages ยท Page 1 of 1 (latest)
@shut ore
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
that's how read works. It consumes the text of the file
what exactly are you trying to do?
Im trying to do it would update what my txt file contains after time and check if it still contains "True" or not
Are you trying to get it to read from the file while you're typing in it?
close() prob fixes it but I doubt it is right to do so
yeah
Yeah, it's not going to detect new changes made to the file once it has been opened by python
it has to be reopened
What are you hoping to achieve with this though?
This doesn't really seem like a practical solve for anything
I plan to make my other lua script change config file in order to stop python loop, but tbh I have no idea how to do it without loop checks(this doesnt seem like right solution as well to me but it is what it is)
and its super hard to work with some libraries in lua so it doesnt seem like it is possible to write my python code there
I know pyside has a file listener so you can assign it a file path and it will emit a signal if the contents of the file change
Here is my code:
f=open("path", "r")
while True:
while f.read()=="True":
print("output is true")
f.close()
time.sleep(0.5)
this gives error apparently
(Io operation on closed file)
from PySide2 import QtCore, QtWidgets
def on_file_change(file):
print(f'{file} has new contents!')
app = QtWidgets.QApplication()
watcher = QtCore.QFileSystemWatcher()
watcher.addPath('foo.txt')
watcher.fileChanged.connect(on_file_change)
app.exec_()
here's an example of doing it with pyside
yeah I might test this as well even tho I try to get rid of extra libs if possible
any time foo.txt is modified at all, the on_file_change function will be called
There's https://pypi.org/project/watchfiles/ if you don't want to install a whole UI toolkit
That's probably better
thank you, I will test it now
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.