xxxxxxxxxx
>>> import os
>>> path=os.path.dirname("C:/folder1/folder2/filename.xml")
>>> path
'C:/folder1/folder2'
>>> os.path.basename(path)
'folder2'
xxxxxxxxxx
import os
#full path
dir_path = os.path.dirname(os.path.realpath(__file__))
#current dir
cwd = os.getcwd()
xxxxxxxxxx
# Basic syntax:
import os
os.path.dirname('/path/to/your/favorite/file.txt')
--> '/path/to/your/favorite/'
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
xxxxxxxxxx
from os import getcwd # only import "getcwd" from os
getcwd() # Get the current working directory