Answers for "python check file type"

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

how to check if object is of file type in python3

# In python3, file objects are part of the io module.
>>> from io import IOBase
>>> f = open(<filePath>, 'w')
>>> isinstance(f, IOBase)
True
>>> isinstance(object(), IOBase)
False
Posted by: Guest on March-31-2021

Python Answers by Framework

Browse Popular Code Answers by Language