Buradasın
Python Methods to Get File Names
pythonguides.com/python-get-filename-from-the-path/Yapay zekadan makale özeti
- Using os Module
- os.path.basename() function extracts file names from paths
- Example: basename('data.txt') from "/home/user/documents/data.txt"
- Using pathlib Module
- Pathlib introduced in Python 3.4 as alternative to os
- Path class provides 'name' attribute for file name extraction
- Example: Path('editor.exe') returns file name from "/home/user/documents/editor.exe"
- Using Regular Expressions
- Regular expressions use pattern matching for file name extraction
- Example: pattern '[w-] +?(?=.)' returns file name without extension
- Requires 're' module's search() function
- Using split() and rsplit()
- Split() divides string into substrings based on specified separator
- rsplit() splits from right side of string
- Example: split('/') followed by rsplit('.') extracts file name
- File name appears at index 0 in list