Answers for "how to check if a file is a text file in python"

2

how to check for a particular word in a text file using python

file = open("search.txt")
    print(file.read())
    search_word = input("enter a word you want to search in file: ")
    if(search_word in file.read()):
        print("word found")
    else:
        print("word not found")
Posted by: Guest on August-10-2020
0

check if file is txt python

Assuming m is a string, you can use endswith:

if m.endswith('.mp3'):
...
elif m.endswith('.flac'):
...
To be case-insensitive, and to eliminate a potentially large else-if chain:

m.lower().endswith(('.png', '.jpg', '.jpeg'))
Posted by: Guest on May-06-2021

Code answers related to "how to check if a file is a text file in python"

Python Answers by Framework

Browse Popular Code Answers by Language