xxxxxxxxxx
import os
# path of the given file
print(os.path.dirname(os.path.abspath("my_file.txt")))
# current working directory
print(os.path.abspath(os.getcwd()))
xxxxxxxxxx
from pathlib import Path
>>> p = r"C:\Users\shali\OneDrive\code\setTheory.pdf"
>>> Path(p).anchor
'C:\\'
>>> Path(p).parent
WindowsPath('C:/Users/shali/OneDrive/code')
>>> Path(p).name
'setTheory.pdf'
>>> Path(p).stem
'setTheory'
>>> Path(p).suffixes
['.pdf']
>>> str(p)
"C:\Users\shali\OneDrive\code\setTheory.pdf"
xxxxxxxxxx
import module_name
path = os.path.dirname(module_name.__file__)
xxxxxxxxxx
import pathlib
# path of the given file
print(pathlib.Path("my_file.txt").parent.absolute())
# current working directory
print(pathlib.Path().absolute())