Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python load relative file path with interactive interpeter

I'm new to python, and having trouble with the basic action of loading a file from a dynamic relative path. I am using Python 3.5 on Spyder 2.3.9 when trying the following:

import os
import sys
print (os.getcwd())
print (os.path.dirname(sys.argv[0]))

I get :

C:\Users\my_user
C:\Anaconda3\lib\site-packages\spyderlib\widgets\externalshell

I am trying to get the location of current running script, so I can load a file (mat file) relative to its path (not same directory). __file__ is not recognized, I think because Spyder uses the interactive interpreter it doesn't recognize it.

like image 900
AlexFink Avatar asked Apr 19 '26 12:04

AlexFink


1 Answers

If you use windows platform, try it: 1- Open the Control Panel (easy way: click in the Windows search on your taskbar and type “Control Panel” then click the icon). 2- In the Control Panel, search for Environment; click Edit the System Environment Variables. Then click the Environment Variables button. 3- In the User Variables section, we will need to either edit an existing PATH variable or create one. If you are creating one, make PATH the variable name and add the following directories to the variable values section as shown, separated by a semicolon. If you’re editing an existing PATH, the values are presented on separate lines in the edit dialog. Click New and add one directory per line. Then write "python" in cmd to recognizing the existence of that is correct or no.

like image 110
Hamed Baziyad Avatar answered Apr 22 '26 04:04

Hamed Baziyad