Explanation:
๐น Line 1: Import Path
from pathlib import Path
Python's modern file-path library:
pathlib
is imported.
Path is used to work with paths like:
"C:/Users/Admin/file.txt"
or
"a/b/c.txt"
in an object-oriented way.
๐น Line 2: Create a Path Object
Path("a/b")
Python creates a path object representing:
a/b
Think of it as:
Folder: a
└── Folder/File: b
Current Path:
Path('a/b')
๐น Line 3: Access .name
Path("a/b").name
.name returns:
The last component of the path
Path:
a/b
Parts:
a
b
Last part:
b
So:
Path("a/b").name
returns:
"b"
๐น Line 4: Print Result
print(Path("a/b").name)
becomes:
print("b")
Output:
b

0 Comments:
Post a Comment