0

I have a program that can be run through the command line. In that program, I need to access a database file (SQLite) and read some data from there. The SQLite file is located on the previous directory of the Python file, therefore I'm accessing it by specifying the path ../mySQLite. When I run the program from the command line, it only works if I'm in the same location as my Python file (So the command would be simply python myProgram.py). However, when I run it from another location (for example I'm on the Desktop and run the command python myProgramDirectory/myProgram.py) it outputs an error that the database file is not found. I understand that this is because I specified the location in the program to be the previous directory of the current location (assuming the user will only run the program if he is on the same location). But I want a way to generalize it such that the program can find the database file regardless of the current location when running the program. Is there a way to perform it?

  • use a full path: the direct location of the database file on the filesystem. – mechanical_meat Jan 11 '22 at 15:19
  • 1
    Does this answer your question? [Get parent of current directory from Python script](https://stackoverflow.com/questions/30218802/get-parent-of-current-directory-from-python-script) Then use the full path to your database. – buran Jan 11 '22 at 15:23
  • @mechanical_meat thank you for your reply, the full path may work only if I'm running the program on a specific machine. So if I install it on another machine I would encounter the same problem again – abdullatif alnajim Jan 11 '22 at 15:30
  • @buran Yes! This is the same problem I have. Thanks you – abdullatif alnajim Jan 11 '22 at 15:31

0 Answers0