1import os
2
3# To get absolute path to current script
4absolutePath = os.path.realpath(__file__)
5print(absoultePath) # c:\File\Path\to\Current\Script.py
6
7# To get absolute path excluding file name
8directory = ("//".join(os.path.realpath(__file__).split('//')[:-1]))
9print(directory) # c:\File\Path\to\Current split('\\') for windows
1#Python 3
2
3#For the directory of the script being run:
4
5import pathlib
6pathlib.Path(__file__).parent.resolve()
7
8#For the current working directory:
9
10import pathlib
11pathlib.Path().resolve()
12
13#Python 2 and 3
14
15#For the directory of the script being run:
16
17import os
18os.path.dirname(os.path.abspath(__file__))
19
20#If you mean the current working directory:
21
22import os
23os.path.abspath(os.getcwd())
1import os
2os.path.abspath(__file__) # Directory of current python script
3#Output will look something like: C:\Users\UserName\Desktop\py-script.py
1from os import getcwd # only import "getcwd" from os
2
3getcwd() # Get the current working directory