Search code examples
pythonqueuereadfilereadlines

How to avoid type error in this case, when the application is closed?


I have the following code, but when I exit from the application, I get: "TypeError: 'in ' requires string as left operand, not NoneType" The thing is I get the hostnames from a Queue, how to solve this to dont get error? Otherwise it is doing its job.

hostname = self.queue.get()
with open("allhosts.txt", "r") as f:
    if hostname in f.read():
        self.found.emit(hostname)
    else:
        self.notFound.emit(hostname)

Solution

  • As per the error you mentioned it should be happening when self.queue.get() is coming as None. So you may need to have an if condition to check if hostname is present or not before proceeding to checking inside the file.