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
#11
Sure, below are the details/algorithm. Please review and advise if I have make any changes to the code.

1. File /root/pydev/serv has server1, server2, server3
cat /root/pydev/serv
server1
server2
server3

2. Expected script function and output
- read the file /root/pydev/serv for its content.
- check the condition, which is "if the serv == server2"
- if the condition matches or equals the contents/line/server from the file then print only that server exclusively. (Example: serv ==server2 then print only "server2" as output.
- if the condition does not matches or equals (example: if condition serv == server5) then only print "No Serv found" in the output.
Reply
#12
if I get you right, because you virtually repeat the same thing again

lookup_server = 'server2'
lookup_file = "/root/pydev/serv"

# option1 
with open(lookup_file, "r") as f:
    servers =  [line.strip() for line in f] 
    if lookup_server in servers:
        print(f'{lookup_server} found in file')
    else:
        print(f'{lookup_server} not found in file')
        

#option2
with open(lookup_file, "r") as f:
    for server in f:
        if server.strip() == lookup_server:
            print(f'{lookup_server} found in file')
            break
    else:
        print(f'{lookup_server} not found in 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
#13
Buran - Thanks again. I have executed the code and the output returned as expected..that's exactly I was looking for. Hope you have wonderful day. Can you please suggest good books or resources for Python so that I can bring myself up to speed in Python.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  unable to remove all elements from list based on a condition sg_python 3 429 Jan-27-2024, 04:03 PM
Last Post: deanhystad
  Triggering a ps1 script in remote windows server via http python request jasveerjassi 1 365 Jan-26-2024, 07:02 PM
Last Post: deanhystad
  Python Alteryx QS-Passing pandas dataframe column inside SQL query where condition sanky1990 0 730 Dec-04-2023, 09:48 PM
Last Post: sanky1990
Sad "PriceSystem" Python Script error to Shopify API Alphetto 0 435 Jul-04-2023, 10:03 AM
Last Post: Alphetto
  Is there a *.bat DOS batch script to *.py Python Script converter? pstein 3 3,200 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,078 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,217 Aug-03-2022, 06:03 AM
Last Post: Pedroski55
  Server Folder Error : WinError5 Access Denied fioranosnake 1 1,116 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,511 Apr-04-2022, 09:29 PM
Last Post: Larz60+
  No module named '_cffi_backend' error with executable not with python script stephanh 2 5,658 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