from pathlib import Path
37 File
38 Path
"C:\\Program Files\\...")
Path(#> PosixPath('C:\\Program Files\\...')
# Combine Path
"A") / Path("B")
Path(#> PosixPath('A/B')
# Home Directory
Path.home()#> PosixPath('/Users/kittipos')
str(Path.home())
#> '/Users/kittipos'
38.1 Path Operation
= Path("file.qmd")
my_path
my_path.exists()#> True
my_path.is_dir()#> False
my_path.is_file()#> True
# Absolute Value
my_path.absolute() #> PosixPath('/Users/kittipos/my_book/py-notes/basic/file.qmd')
# File name
my_path.name #> 'file.qmd'
# File name, no ext
my_path.stem #> 'file'
# Ext
my_path.suffix #> '.qmd'
# Parent Folder
my_path.parent #> PosixPath('.')
Change Name, Suffix, Stem
= my_path.with_name("file.txt")
my_path print(my_path)
#> file.txt
= my_path.with_suffix(".py")
my_path print(my_path)
#> file.py