0

this is the Directory its looking in:

'C:\Users\MyUsername\AppData\Local\Temp\SomeTempfile\TheDirectory\../sources/sources.csv'

it should be going:

C:\Users\MyUsername\Desktop\Pythonprogram\sources/sources.csv

Error Message:

Exception in Tkinter callback Traceback (most recent call last):

File "Directory\PythonFile.py", line 22, in pythonFunction FileNotFoundError: [Errno 2]

No such file or directory:

'C:\Users\Username\AppData\Local\Temp\SomeTempfile\MyFile\../sources/sources.csv'

the code im using is:

    script_dir = os.path.dirname(__file__)  # Script directory
    full_path = os.path.join(script_dir, '../sources/sources.csv')
    csv_file = open(full_path)
    csv_table = list(csv.reader(csv_file, delimiter=';'))

I am using Pyinstaller to create the .exe file.

notDaniel
  • 17
  • 5
  • Try using \\ instead of / as the path seperator in the `join` call. – B Remmelzwaal Feb 03 '23 at 14:53
  • double backlash does not seem to help. No difference in script file same error message in .exe file – notDaniel Feb 03 '23 at 15:25
  • I see now that you are creating an .exe file which is probably what explains it trying to find something in the Temp folder. In this instance I would hard code the location of the folder with the .csv file. – B Remmelzwaal Feb 03 '23 at 15:38
  • I cant hardcode it because it has to run on multiple devices sadly – notDaniel Feb 03 '23 at 15:49
  • Then you should find a workaround, because if the file is not in the same location and not easily relative to the executable, it becomes quite difficult to do it this way. Is there any pattern to the location of the .csv? – B Remmelzwaal Feb 03 '23 at 15:54
  • But why does he go in the temp file? The exe file is not in there. That does not make sense. Where is my mistake? – notDaniel Feb 03 '23 at 15:56
  • Yeah the pattern is, that the csv file is in the directory sources where the scriptfile is... – notDaniel Feb 03 '23 at 15:57
  • 1
    Maybe [this](https://stackoverflow.com/a/404750/17200348) can help? – B Remmelzwaal Feb 03 '23 at 15:58

1 Answers1

0

Just so the issue can be closed, this answer solved the problem.

import os
import sys

# determine if application is a script file or frozen exe
if getattr(sys, 'frozen', False):
    script_dir = os.path.dirname(sys.executable)
elif __file__:
    script_dir = os.path.dirname(__file__)
B Remmelzwaal
  • 1,581
  • 2
  • 4
  • 11