Answers for "python check file ending"

3

python check file extension

# Checking for single extension (.txt) of file
if file.endswith(".txt"):
  # do something
  
# Checking multiple extensions (.txt, .pdf, .mp3, ...)
if file.endswith((".txt", ".pdf", ".mp3")):
  # do something
Posted by: Guest on November-04-2020
0

python get file ending

import os
# creates a tuple.
# fn contains the filename, in this case abc
# fend contains the file ending, in this case .txt
fname, fend = os.path.splitext("/Users/pankaj/abc.txt")
Posted by: Guest on March-26-2021

Python Answers by Framework

Browse Popular Code Answers by Language