xxxxxxxxxx
from os import getcwd # only import "getcwd" from os
getcwd() # Get the current working directory
xxxxxxxxxx
import os
#full path
dir_path = os.path.dirname(os.path.realpath(__file__))
#current dir
cwd = os.getcwd()
xxxxxxxxxx
>>> import os
>>> path=os.path.dirname("C:/folder1/folder2/filename.xml")
>>> path
'C:/folder1/folder2'
>>> os.path.basename(path)
'folder2'
xxxxxxxxxx
#Python 3
#For the directory of the script being run:
import pathlib
pathlib.Path(__file__).parent.resolve()
#For the current working directory:
import pathlib
pathlib.Path().resolve()
#Python 2 and 3
#For the directory of the script being run:
import os
os.path.dirname(os.path.abspath(__file__))
#If you mean the current working directory:
import os
os.path.abspath(os.getcwd())
xxxxxxxxxx
import os
your_file_path = "D:\\your\\dir\\path\\file.csv"
print(f"Dir:{os.path.dirname(your_file_path)}")
#>>> Dir:D:\\your\\dir\\path
xxxxxxxxxx
from os import getcwd
print(getcwd())
#This prints the directory like C:\users\hp\projects