The Python Oracle

Find the current directory and file's directory

Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn

--

Music by Eric Matyas
https://www.soundimage.org
Track title: Underwater World

--

Chapters
00:00 Question
00:25 Accepted answer (Score 4469)
01:54 Answer 2 (Score 371)
02:19 Answer 3 (Score 345)
02:45 Answer 4 (Score 262)
05:21 Thank you

--

Full question
https://stackoverflow.com/questions/5137...

Accepted answer links:
[os]: https://docs.python.org/library/os.html
[os.path]: https://docs.python.org/library/os.path....
[__file__]: https://docs.python.org/reference/datamo...
[os.path.realpath(path)]: https://docs.python.org/library/os.path....
[os.path.dirname(path)]: https://docs.python.org/library/os.path....
[os.getcwd()]: https://docs.python.org/library/os.html#...
[os.chdir(path)]: https://docs.python.org/library/os.html#...

Answer 2 links:
[Current working directory]: https://sourceware.org/gdb/onlinedocs/gd...
[os.getcwd()]: https://docs.python.org/2/library/os.htm...
[__file__]: http://docs.python.org/reference/datamod...
[How do I get the path of the current executed file in Python?]: https://stackoverflow.com/questions/2632...

Answer 4 links:
[pathlib]: https://docs.python.org/3/library/pathli...
[introduced in Python 3.4]: https://docs.python.org/3/whatsnew/3.4.h...
[PEP 428 — The pathlib module — object-oriented filesystem paths]: https://www.python.org/dev/peps/pep-0428/
[Path.cwd()]: https://docs.python.org/3/library/pathli...
[Path.resolve()]: https://docs.python.org/3/library/pathli...
[.parent]: https://docs.python.org/3/library/pathli...
[How do I get the path of the current executed file in Python?]: https://stackoverflow.com/questions/2632...
[PosixPath]: https://docs.python.org/3/library/pathli...
[open]: https://docs.python.org/3.5/library/func...
[Path.open()]: https://docs.python.org/3/library/pathli...
[PEP 519 — Adding a file system path protocol]: https://www.python.org/dev/peps/pep-0519/
[PathLike]: https://docs.python.org/3/library/os.htm...
[open]: https://docs.python.org/3/library/functi...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #directory

#avk47



ACCEPTED ANSWER

Score 4781


To get the full path to the directory a Python file is contained in, write this in that file:

import os 
dir_path = os.path.dirname(os.path.realpath(__file__))

(Note that the incantation above won't work if you've already used os.chdir() to change your current working directory, since the value of the __file__ constant is relative to the current working directory and is not changed by an os.chdir() call.)


To get the current working directory use

import os
cwd = os.getcwd()

Documentation references for the modules, constants and functions used above:




ANSWER 2

Score 391


Current working directory: os.getcwd()

And the __file__ attribute can help you find out where the file you are executing is located. This Stack Overflow post explains everything: How do I get the path of the current executed file in Python?




ANSWER 3

Score 368


You may find this useful as a reference:

import os

print("Path at terminal when executing this file")
print(os.getcwd() + "\n")

print("This file path, relative to os.getcwd()")
print(__file__ + "\n")

print("This file full path (following symlinks)")
full_path = os.path.realpath(__file__)
print(full_path + "\n")

print("This file directory and name")
path, filename = os.path.split(full_path)
print(path + ' --> ' + filename + "\n")

print("This file directory only")
print(os.path.dirname(full_path))



ANSWER 4

Score 84


  1. To get the current directory full path

    >>import os
    >>print os.getcwd()
    

    Output: "C :\Users\admin\myfolder"

  2. To get the current directory folder name alone

    >>import os
    >>str1=os.getcwd()
    >>str2=str1.split('\\')
    >>n=len(str2)
    >>print str2[n-1]
    

    Output: "myfolder"