0

How to access the file:

import os
os.chdir(os.path.dirname(os.path.abspath(__file__)))

def main():
    with open("pass.txt", "r") as pf:  ## This is line 51 in the screenshot
        passwds = pf.read().splitlines()
    
    # some codes

if __name__ == "__main__":
    main()  ## This is line 83 in the screenshot

I used this

pyinstaller --clean --onefile start.py

Screenshot: Note that the files are located next to the executable file

Traceback (most recent call last):
  File "start.py", line 83, in <module>
  File "start.py", line 51, in main
FileNotFoundError: [Errno 2] No such file or directory: 'pass.txt'
[50416] Failed to execute script 'start' due to unhandled exception!

When i used:

pyinstaller start.py

That is, it is not in one file, it was working without problems, but there are many files next to it, and I do not want this

But when I use make it to one file --onefile the problem occurs

How can this problem be solved?

martineau
  • 119,623
  • 25
  • 170
  • 301
Mr.Neo
  • 1
  • 2
  • We need to see the line(s) of code that is/are causing the error — i.e. how is `pass.txt` be accessed? – martineau Feb 19 '22 at 03:48
  • I added it to the question, check again – Mr.Neo Feb 19 '22 at 05:05
  • The problem has to do with your use of `__file__` to change the directory. See pyinstaller's documentation on [Using \_\_file__](https://pyinstaller.readthedocs.io/en/stable/runtime-information.html#using-file). – martineau Feb 19 '22 at 10:33
  • I used it and the result is like this `FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\Mr.Neo\\AppData\\Local\\Temp\\_MEI320802\\pass.txt'` – Mr.Neo Feb 19 '22 at 15:38
  • It's `sys._MEIPASS`. – martineau Feb 19 '22 at 16:14
  • What does this mean? – Mr.Neo Feb 19 '22 at 17:38
  • I also tried [this](https://stackoverflow.com/questions/7674790/bundling-data-files-with-pyinstaller-onefile/13790741#13790741) but the result is the same: `FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\Mr.Neo\\AppData\\Local\\Temp\\_MEI237802\\pass.txt'` – Mr.Neo Feb 19 '22 at 17:47
  • Try `os.chdir(sys._MEIPASS)`. Another possibility is `os.chdir(os.path.dirname(sys.argv[0]))`. If those don't work, I have no further ideas. – martineau Feb 19 '22 at 17:51
  • I found the solution [here](https://stackoverflow.com/a/404750/18248481) thanks for your efforts I really appreciate you martineau <3 – Mr.Neo Feb 19 '22 at 18:23

1 Answers1

0

The solution is here

thanks for your efforts I really appreciate you martineau <3

Mr.Neo
  • 1
  • 2
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 19 '22 at 21:46