For loop problem!
-
Dear All,
The PROCESS function below, I have "For loop". The first if statement is working fine but the second if statement is not execute at all. It has been changed in so many ways with no success.
Any ideas,
Many thanks,
def Buttons(self): self.press_4.clicked.connect(self.Process) def Process(self): for proc in psutil.process_iter(): x=self.lineEdit.text() x2= "<font color='#00ff00'> "+x+"</font>" if proc.name() not in x: QMessageBox.information(self,"Error",'Blank!') return if proc.name() in x: proc.kill() QMessageBox.information(self, "Success", x2 +"has been terminated" return
-
@monamour Looks like you edited the code, because before both if were identical.
Actually you can do it like this:if proc.name() not in x: QMessageBox.information(self,"Error",'Blank!') return else: proc.kill() QMessageBox.information(self, "Success", x2 +"has been terminated" return
-
1- proc.name after "IF CONDITIONS"
input
print(x)
print(proc.name())Output
notepad.exe
notepad.exe2-The full proc.name after "FOR LOOP"
input
print(proc.name())
output
System Idle Process
System
svchost.exe
smss.exe
csrss.exe
MOM.exe
wininit.exe
csrss.exe
services.exe
lsass.exe
lsm.exe
python.exe
explorer.exe
svchost.exe
svchost.exe
chrome.exe
WSHelper.exe
cbInterface.exe
taskhost.exe
notepad.exe
PresentationFontCache.exe
SearchFilterHost.exe
chrome.exe
chrome.exe
designer.exe
thunderbird.exe
fsnotifier64.exe
conhost.exe
chrome.exe
svchost.exe
notepad++.exe
VSSVC.exe
pycharm64.exe
chrome.exe
chrome.exe
SearchProtocolHost.exe
conhost.exe
chrome.exeProcess finished with exit code 0
-
@monamour said in For loop problem!:
Means? because both of them are strings
I mean different string encodings like Latin1, UTF-8, ...
Can you try to print out type(x) and type(proc.name())?
"If I make two for loops, it will work. What is the common sense in this ?" - I don't know what you mean. -
for proc in psutil.process_iter(): if proc.name()==x: proc.kill() QMessageBox.information(self, "Success", x2 +"has been terminated") return for proc in psutil.process_iter(): if proc.name()!=x: QMessageBox.information(self, "Error","The process " +x2 + " is not found") return