You can use the `os.path.basename()` function in Python's `os` module to get the basename
(filename) of a full path:
python
import os
full_path = "/path/to/file.txt"
basename = os.path.basename(full_path)
print(basename) # Output: file.txt
Alternatively, you can use string manipulation methods like slicing or splitting to
extract the filename from the full path:
python
full_path = "/path/to/file.txt"
filename = full_path.split("/")[-1]
print(filename) # Output: file.txt
However, using `os.path.basename()` is generally more concise and robust, as it handles
edge cases like trailing slashes or directory names with embedded slashes.
If you want to get the basename without any extension (e.g., "file" from "file.txt"), you
can use the `os.path.splitext()` function:
python
import os
full_path = "/path/to/file.txt"
filename, _ = os.path.splitext(os.path.basename(full_path))
print(filename) # Output: file
Note that in this example, we're using tuple unpacking to extract just the filename
(without extension) from the `splitext()` result.