I am working on a Flask blog application, and I am trying to separate a file extension from a filename. For example, for the filename "IMG_0503.jpg," I would like to get "IMG_0503" and ".jpg" separately. I tried the pathlib module and the os.path.splitext() method, but both return an empty file extension instead of ".jpg"
Here is my code with os.path.splitext():
uploaded_file = request.files['file']
print("UPLOADED_FILE:", uploaded_file)
filename = secure_filename(uploaded_file.filename)
print("FILENAME:", filename)
filename = os.path.splitext(filename)[0]
file_ext = os.path.splitext(filename)[1]
print("OS.PATH.SPLITEXT:", os.path.splitext(filename))
print("FILE_EXT:", file_ext)
This is the output of print statements:
UPLOADED_FILE: <FileStorage: 'IMG_0503.jpg' ('image/jpeg')>
FILENAME: IMG_0503.jpg
OS.PATH.SPLITEXT: ('IMG_0503', '')
FILE_EXT:
With the pathlib, the output is exactly the same, but I am using file_ext = pathlib.Path(filename).suffix
to get the extension.
Can someone please help me to resolve this issue?