Python Forum

Full Version: Python script Server list - if condition error
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
I have below script where I am getting error after executing. Can any help in resolving errors.

#!/usr/bin/env python

import os

servers = os.system("cat /root/pydev/serv")   # =====> /root/pydev/serv has list of servers.

for serv in servers:
 if serv == "server01":
   print(serv)
 else:
   print("No Serv found")
 break
Output error:-
Error:
server01 server02 server03 Traceback (most recent call last): File "./loops.py", line 9, in <module> for serv in servers: TypeError: 'int' object is not iterable
os.system returns the exit code, so this code behaves exactly as one might expect. Why use the os module? Why not just open the file like you would normally in Python?
Thanks for suggestions micseydel. I have updated to code to below and even I am not getting the anticipated output. I am kind of new to python and in learning stage. Can you anyone please advise what changes can be made.

servers = open("/root/pydev/serv", "r")

for serv in servers:
 if serv == "server1":
   print(serv)
 else:
   print("No Serv found")
   break
Output:
(py36-venv)# ./loops.py No Serv found
Anticipated output was - server1
my code is running correctly if I use below. But I want to use the unix/linux command to list the servers and then use the IF condition to perform particular task. In the below code I don't want to input server list which are big list, but I would like to use the command to get the list from file and then apply the IF condition to perform the task. Any suggestions would be greatly appreciated.

servers = ["server1","server2","server3"]
with open("/root/pydev/serv", "r") as servers:
    for serv in servers:
        if serv.strip() == "server1": # or if serv.startswith('server'):
            print(serv)
        else:
            print("No Serv found")
            break
when you read from file, each line has the lineending \n, so serv is never equal to 'server1'
Buran - Thank you very much Smile. The given code change worked well and gave anticipated output. Can you recommended any good books or resource for learning Python for Linux/Unix Admins so that I can integrate commands in code and also for reports generation and other etc... features. I am primarily Bash and Korn user and want to switch to Python for my code development. Please advise.
Seems like there is a problem with below. I have tested other server variable with "server2" and the script is failing. Can anyone please advise.

with open("/root/pydev/serv", "r") as servers:
    for serv in servers:
        if serv.strip() == "server2": # or if serv.startswith('server'):
            print(serv)
        else:
            print("No Serv found")
            break
Error:
No Serv found
you are checking just the first line, if it is not what you expect, you print No Serv found and exit the loop (because of the break)
To avoid further fruitless iterations, please explain what you actually want...
I would like to check or read all the lines in the file "/root/pydev/serv" for matching then in the if condition if the variable matches any given server from file then print that particular server name which == to. If the serv == server4 then only I am expecting "No Serv found" as output.

File /root/pydev/serv has below
server1
server2
server3
sorry, but it's not clear

you have a list of servers and a file. So you want to check if any/which of the servers in the list are present in the file or you want to check if any/which entries in the file are also present in the list? Also if you just print No Serv found, how you know which serv is not found.
Or maybe you want to check if one SPECIFIC server is in the file?
Pages: 1 2