File reading issue in Python
Solved
Erwan2212
Posted messages
14
Status
Member
-
Erwan2212 Posted messages 14 Status Member -
Erwan2212 Posted messages 14 Status Member -
Hello everyone,
I coded an application in Python a few days ago, and I have a problem that seems silly but I can't solve it and I'm coming to you for help. The issue is that Python cannot find the file named 'Licence.txt'; the strangest thing is that it is in the same directory, and I don't know why.
Here is my code:
and here is the error message:
If anyone has an idea, it would be greatly appreciated.
Thank you in advance.
I coded an application in Python a few days ago, and I have a problem that seems silly but I can't solve it and I'm coming to you for help. The issue is that Python cannot find the file named 'Licence.txt'; the strangest thing is that it is in the same directory, and I don't know why.
Here is my code:
self.textLicence.setVisible(False)
self.text = open("Licence.txt","r")
self.text.read()
self.textLicence.setPlainText(self.text)
and here is the error message:
File "/home/erwan/Desktop/Projet/Class/appr_main.py", line 17, in traitement
self.text = open("Licence.txt","r")
FileNotFoundError: [Errno 2] No such file or directory: '/Licence.txt'
If anyone has an idea, it would be greatly appreciated.
Thank you in advance.
16 answers
-
Hello Erwan,
Ideally, you should include your complete code, and also use the tags to make it more readable:
https://codes-sources.commentcamarche.net/faq/11288-les-balises-de-code -
Hello Phil, yes
from PyQt5.QtGui import * from PyQt5.QtWidgets import * from PyQt5.QtCore import * from .apr import Ui_Form import os class appr(QWidget, Ui_Form): def __init__(self, parent=None): super(appr, self).__init__(parent) self.setupUi(self) self.center() self.traitement() self.action() def traitement(self): self.textLicence.setVisible(False) self.text = open("Licence.txt","r") self.text.read() self.textLicence.setPlainText(self.text) def action(self): self.licence.clicked.connect(self.showLicence) def showLicence(self): self.textLicence.setVisible(True) self.lab3.setVisible(False) def center(self): qr = self.frameGeometry() cp = QDesktopWidget().availableGeometry().center() qr.moveCenter(cp) self.move(qr.topLeft())
and the main file outside of this folder.
Thank you very much for getting back to me, Phil. -
ok, but the code is not complete, there is only the definition of a class ...
-
Yes, the Ui_form is the interface class via qtdesigner and should I post that too?
-
I don't know ...
What I mean is that your program is not just the definition of a class,
but there's more to it than that, right?
The program has to start somewhere; you must create an object from the class and then use it ...
What's in appr_main.py? -
Yes, that's all there is,
it's the code just above in appr_main.py
I'm lost Phil, I can't solve it up to now. -
And your file is named Licence.txt with a capital L?
And is it in the same directory as appr_main.py?
And what if you used the full path to see:
self.text = open("C:\\Phil\\Licence.txt","r") -
Yes, everything is normal, but I don't know why Python can't find my file.
I tried like this but still nothingself.text = open("~/Desktop/Project/Class/Licence.txt","r") -
Whoa!
Try using the actual full path:
C:\\Users\\erwan\\Desktop\\Project\\Class\\Licence.txt
if Erwan is indeed your username on your computer.
For me, it looks like this:
-
Yes, I've done everything, but it only brings me failures. I'm on Lubuntu 20.04. Don't you have any other idea?
-
Ah OK you are on Linux, you should have said that right away
I am on Windows .... -
-
No, I don't know Linux...
-
-
The ideal when asking a question is to immediately indicate which operating system
we are on and which version of Python ... -