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 pathlib
# path of the given file
print(pathlib.Path("my_file.txt").parent.absolute())
# current working directory
print(pathlib.Path().absolute())
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
data_folder = "source_data/text_files/"
file_to_open = data_folder + "raw_data.txt"
f = open(file_to_open)
print(f.read())