Check if file name contains Python string

I am trying to find a way when the file name that the program reads will be checked if it contains any of the lines, as shown below. I am not sure if this is the right way. The string will be a global variable, since 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

+4
source share
2 answers

Try the following:

 def check_word_type(self, filename): words = ['noun','verb','vrb','adj','adv'] #I am not sure if adj and adv are variables self.word_type = '' for i in words: if i in filename: self.word_type = str(i) #just make sure its string return self.word_type 
+8
source

You do not call check_word_type() anywhere. Try:

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

Source: https://habr.com/ru/post/1489745/


All Articles