To extract the filename without the extension from a path in Python, you can use either the os.path module (for legacy code) or the modern pathlib module. Here’s how to do it:
Method 1: Using pathlib (Python 3.4+)
from pathlib import Path
file_path = "/path/to/your/file.txt"
filename = Path(file_path).stem # Extracts "file" from "file.txt"
print(filename) # Output: "file"
Example with multiple extensions:
file_path = "/path/to/archive.tar.gz"
filename = Path(file_path).stem # Extracts "archive.tar" (removes last extension)
print(filename) # Output: "archive.tar"
Method 2: Using os.path (Legacy Approach)
import os
file_path = "/path/to/your/file.txt"
base_name = os.path.basename(file_path) # Extracts "file.txt"
filename = os.path.splitext(base_name)[0] # Splits into ("file", ".txt")
print(filename) # Output: "file"
Example with multiple extensions:
file_path = "/path/to/archive.tar.gz"
base_name = os.path.basename(file_path) # "archive.tar.gz"
filename = os.path.splitext(base_name)[0] # ("archive.tar", ".gz") → "archive.tar"
print(filename)
Key Notes
- Removes Only the Last Extension
Both methods strip the last extension (e.g.,.txtinfile.txt,.gzinfile.tar.gz). - Handles Edge Cases:
- Paths with no extension:
/path/to/README→README. - Hidden files (Unix):
.bashrc→.bashrc(no extension removed). - Paths with directories:
/path/to/file.txt→file.
Remove All Extensions (Optional)
If you want to remove all extensions (e.g., file.tar.gz → file), split iteratively:
from pathlib import Path
file_path = "/path/to/archive.tar.gz"
filename = Path(file_path).stem # "archive.tar"
while filename != (filename := Path(filename).stem):
pass
print(filename) # Output: "archive"
Comparison
| Method | Pros | Cons |
|---|---|---|
pathlib | Modern, readable, OOP-based | Requires Python 3.4+ |
os.path | Backward-compatible | Less intuitive for new code |
Final Recommendation
Use pathlib for new code—it’s cleaner and handles paths more elegantly. For legacy code, stick with os.path.