Python Forum
Python script Server list - if condition error
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python script Server list - if condition error
#1
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
Reply
#2
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?
Reply
#3
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
Reply
#4
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"]
Reply
#5
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'
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#6
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.
Reply
#7
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
Reply
#8
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...
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#9
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
Reply
#10
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?
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  unable to remove all elements from list based on a condition sg_python 3 377 Jan-27-2024, 04:03 PM
Last Post: deanhystad
  Triggering a ps1 script in remote windows server via http python request jasveerjassi 1 321 Jan-26-2024, 07:02 PM
Last Post: deanhystad
  Python Alteryx QS-Passing pandas dataframe column inside SQL query where condition sanky1990 0 690 Dec-04-2023, 09:48 PM
Last Post: sanky1990
Sad "PriceSystem" Python Script error to Shopify API Alphetto 0 415 Jul-04-2023, 10:03 AM
Last Post: Alphetto
  Is there a *.bat DOS batch script to *.py Python Script converter? pstein 3 3,009 Jun-29-2023, 11:57 AM
Last Post: gologica
Question Need help for a python script to extract information from a list of files lephunghien 6 1,033 Jun-12-2023, 05:40 PM
Last Post: snippsat
  How to modify python script to append data on file using sql server 2019? ahmedbarbary 1 1,177 Aug-03-2022, 06:03 AM
Last Post: Pedroski55
  Server Folder Error : WinError5 Access Denied fioranosnake 1 1,093 Jun-21-2022, 11:11 PM
Last Post: Larz60+
  select Eof extension files based on text list of filenames with if condition RolanRoll 1 1,475 Apr-04-2022, 09:29 PM
Last Post: Larz60+
  No module named '_cffi_backend' error with executable not with python script stephanh 2 5,559 Nov-25-2021, 06:52 AM
Last Post: stephanh

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020