File reading issue in Python

Solved
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 answers

  1. The ideal when asking a question is to immediately indicate which operating system

    we are on and which version of Python ...
    1. Ah OK you are on Linux, you should have said that right away

      I am on Windows ....
      1. Yes, I've done everything, but it only brings me failures. I'm on Lubuntu 20.04. Don't you have any other idea?
        1. 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:
          1. 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")
            1. 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")
              1. 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.
                1. 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?
                  1. Yes, the Ui_form is the interface class via qtdesigner and should I post that too?
                    1. ok, but the code is not complete, there is only the definition of a class ...
                      1. 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.