File reading issue in Python

Solved
Erwan2212 Posted messages 14 Status Membre -  
Erwan2212 Posted messages 14 Status Membre -
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:

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 réponses

Phil_1857 Posted messages 1883 Registration date   Status Membre Last intervention   169
 
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
0
Erwan2212 Posted messages 14 Status Membre
 
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.
0
Phil_1857 Posted messages 1883 Registration date   Status Membre Last intervention   169
 
ok, but the code is not complete, there is only the definition of a class ...
0
Erwan2212 Posted messages 14 Status Membre
 
Yes, the Ui_form is the interface class via qtdesigner and should I post that too?
0
Phil_1857 Posted messages 1883 Registration date   Status Membre Last intervention   169
 
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?
0
Erwan2212 Posted messages 14 Status Membre
 
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.
0
Phil_1857 Posted messages 1883 Registration date   Status Membre Last intervention   169
 
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")
0
Erwan2212 Posted messages 14 Status Membre
 
Yes, everything is normal, but I don't know why Python can't find my file.
I tried like this but still nothing
self.text = open("~/Desktop/Project/Class/Licence.txt","r")
0
Phil_1857 Posted messages 1883 Registration date   Status Membre Last intervention   169
 
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:
0
Erwan2212 Posted messages 14 Status Membre
 
Yes, I've done everything, but it only brings me failures. I'm on Lubuntu 20.04. Don't you have any other idea?
0
Phil_1857 Posted messages 1883 Registration date   Status Membre Last intervention   169
 
Ah OK you are on Linux, you should have said that right away

I am on Windows ....
0
Erwan2212 Posted messages 14 Status Membre
 
Okay Phil, do you have any other solutions please?
0
Phil_1857 Posted messages 1883 Registration date   Status Membre Last intervention   169
 
No, I don't know Linux...
0
Erwan2212 Posted messages 14 Status Membre
 
ok thank you Phil
0
Phil_1857 Posted messages 1883 Registration date   Status Membre Last intervention   169
 
The ideal when asking a question is to immediately indicate which operating system

we are on and which version of Python ...
0
Erwan2212 Posted messages 14 Status Membre
 
ok, I am on Lubuntu 20.04 and python3.8
0