Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if the filename contains a string Python

Tags:

People also ask

How do you check if a letter is in a string in Python?

Python String isalpha() The isalpha() method returns True if all characters in the string are alphabets. If not, it returns False.

How do I find a substring in a string Python?

Python String find() method returns the lowest index or first occurrence of the substring if it is found in a given string. If it is not found, then it returns -1. Parameters: sub: Substring that needs to be searched in the given string.


I am trying to find a way where the name of the file the program is reading will be checked if it contains any of the strings like below. I am not sure if that is the right way to go about it. The string is going to be a global variable as I have to use it later in the program

class Wordnet():

    def __init__(self):
        self.graph = Graph()
        self.filename = ''
        self.word_type = ''

    def process_file(self):
        self.filename = "noun.txt"
        self.file = open(self.filename, "r")
        return self.file, self.filename

    def check_word_type(self, filename):
        if 'noun' in filename:
            self.word_type = 'noun'
        elif 'verb' in filename:
            self.word_type = 'verb'
        elif 'vrb' in filename:
            self.word_type = 'verb'
        elif adj in filename:
            self.word_type = 'adj'
        elif adv in filename:
            self.word_type = 'adv'
        else:
            self.word_type = ''
        return self.word_type

if __name__ == '__main__':
    wordnet = Wordnet()
    my_file = wordnet.process_file()  
    print wordnet.word_type

Any help would be great