This feature is implemented in os.path.splitext(path). splitext(path) returns the result of dividing the path by = root + ext.
Note that ext can be an empty string or can start with '.'
import os
fname, ext = os.path.splitext('/path/mytxt.txt')
print fname
print ext
fname, ext = os.path.splitext('.path/mytxt.txt')
print fname
print ext
Results)
/path/mytxt
.txt
.path/mytxt
.txt
© 2024 OneMinuteCode. All rights reserved.