-1

I want to show the directory of my script but is not working with the answers I've founded.

I've tried with several answers, but this one was the most repeated one:

import os
os.path.dirname(os.path.realpath('__file__'))

Still it isn't giving me the desired output, is giving me the working directory:

C:\Users\my_user\Desktop

But I'm working inside a folder in desktop, desired output:

C:\Users\my_user\Desktop\working_folder
Ipa
  • 109
  • 1
  • 8
  • 3
    Possible duplicate of [How to properly determine current script directory?](https://stackoverflow.com/questions/3718657/how-to-properly-determine-current-script-directory) – Murali Aug 27 '19 at 08:54
  • Maybe you are looking for [this](https://stackoverflow.com/a/5137509/5665958)? – Tankred Aug 27 '19 at 08:54
  • those solutions are not working, that's why I posted a different topic – Ipa Aug 27 '19 at 08:55
  • What do you mean by "not working", `os.getcwd()` gives you the working directory. If it is giving you a different directory than you expected, then your working directory is not what you think – FlyingTeller Aug 27 '19 at 08:56
  • I don't want the working directory, I want the directory of the file. When I open the file I want to set the location of the file as the working directory. – Ipa Aug 27 '19 at 08:59
  • Can you add the path where your scripts are located? – Uli Sotschok Aug 27 '19 at 09:15
  • C:\Users\my_user\Desktop\working_folder – Ipa Aug 27 '19 at 09:33
  • C:\Users\my_user\Desktop\working_folder\script.py – Ipa Aug 27 '19 at 09:34

2 Answers2

1

Pretty sure i got it right now :

import os
path = os.path.realpath(__file__) 
filename= os.path.basename(__file__) 
print(os.path.join(os.path.dirname(path), filename))
Oka
  • 101
  • 6
  • I get: NameError: name '__ file __ ' is not defined – Ipa Aug 27 '19 at 09:32
  • it's the dunder, `__file__` – Oka Aug 27 '19 at 09:33
  • are you trying from shell directly ? try with a python file that you exec then it'll work ! ( the __file__ var is set thanks to filepath, which is not set in shell – Oka Aug 27 '19 at 09:37
  • Okay, from the shell is working! Why is not working on Atom editor? – Ipa Aug 27 '19 at 09:39
  • If I use the shell is working, but with Atom editor is not. – Ipa Aug 27 '19 at 09:44
  • what do you mean by atom editor ? Are you executing your file from it ? or manually ? because if you wrote a proper file with the shebang like this it must work `#! /usr/bin/python3 import os path = os.path.realpath(__file__) filename = os.path.basename(__file__) print(os.path.join(os.path.dirname(path), filename))` – Oka Aug 27 '19 at 09:49
  • it's strange, maybe the package is not executing the python code the "conventional way", if you can try to run your code directly from shell `python3 myfile.py ` for example, we will know if it's because of hydrogen – Oka Aug 27 '19 at 11:08
  • Can it be because is executing ipython? – Ipa Aug 27 '19 at 13:23
  • yup i checked and the same problem occurs, with Ipython and shell it's the same reason, the variable can't be initialized, it must be ran in a file :) – Oka Aug 27 '19 at 13:30
0

print os.path.realpath(__file__) is what you need i think please check this link for more on printing filepath How do I get the path and name of the file that is currently executing?

RAFIQ
  • 905
  • 3
  • 18
  • 32