Python: Comparison is not working for me


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Python: Comparison is not working for me
# 1  
Old 02-04-2014
Python: Comparison is not working for me

Guys it is very simple script , but for some reason my IF condition is not matching.

Code:
from sys import argv
import os
filename='testdata.txt'

txt=open(filename)
while 1:
 
 line= txt.readline()
 if not line:
	break
 line=line.strip('\n')
 sp=line.split(" ")
 command =sp[2]+ " " +sp[3]+ " " +sp[4]
 print command
 re=os.system(command)
 print "output:",re
 print "expected:",sp[0]
 if re == exp:
  print "Testcase Passed", sp[1]
 else:
  print "Testcase Failed", sp[1]

the if condition is always failing for me....
# 2  
Old 02-04-2014
Where do you set exp? ...
# 3  
Old 02-04-2014
opps my mistake the code is actually

Code:
from sys import argv
import os
filename='testdata.txt'

txt=open(filename)
while 1:
 
 line= txt.readline()
 if not line:
	break
 line=line.strip('\n')
 sp=line.split(" ")
 command =sp[2]+ " " +sp[3]+ " " +sp[4]
 print command
 re=os.system(command)
 print "output:",re
 print "expected:",sp[0]
 if sp[0] == re:
  print "Testcase Passed", sp[1]
 else:
  print "Testcase Failed", sp[1]

i got the indication of failure , the out put of os.system is 1 and sp[0] is '1'.
how i can handle such conditions..?
# 4  
Old 02-04-2014
probably because of the data types. try converting sp[0] to an integer as well:

Code:
 if int(sp[0]) == re:

This User Gave Thanks to neutronscott For This Post:
# 5  
Old 02-04-2014
that worked , thanks Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Programming

Create a C source and compile inside Python 1.4.0 to 3.7.0 in Python for ALL? platforms...

Hi all... As you know I like making code backwards compatible for as many platforms as possible. This Python script was in fact dedicated for the AMIGA A1200 using Pythons 1.4.0, 1.5.2, 1.6.0, 2.0.1, and 2.4.6 as that is all we have for varying levels of upgrades from a HDD and 4MB FastRam... (1 Reply)
Discussion started by: wisecracker
1 Replies

2. Programming

Python (seleniumrequests)Class, Constructor, Method Not Working

Newbie question. I created a class: class WP(seleniumrequests.PhantomJS): def __init__(self, wpurl='https://blah.org/download/release-archive/', bwppurl='https://blah.org/plugins/browse/popular/'): self.wp=wpurl ... (5 Replies)
Discussion started by: metallica1973
5 Replies

3. Programming

Java HTTP PUT Request/JSON Not Working But Using Python It Does ?

I have some code that I have been playing around with learning Java: import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; ... (1 Reply)
Discussion started by: metallica1973
1 Replies

4. Shell Programming and Scripting

**python** unable to read the background color in python

I am working on requirement on spreadsheet in python scripting. I have a spreadsheet containing cell values and with background color. I am able to read the value value but unable to get the background color of that particular cell. Actually my requirement is to read the cell value along... (1 Reply)
Discussion started by: giridhar276
1 Replies

5. Shell Programming and Scripting

Date Format not working in python script

Good Morning, I am working on a pyhton script, where it has to fetch a file from our vendor's server over an ftp. The file is having a date format of "MMDDYYYY" (i have included in my python script), but when the script is executed it is unable to fetch the file. At the same time, if i... (1 Reply)
Discussion started by: AReddy
1 Replies

6. Shell Programming and Scripting

awk comparison not working

Can you please help me on belw awk comparsion which doest not work cat employee_list NAME Last-login Jack 03/25/2013 Maneul 03/26/2013 Eric 03/26/2013 Samuel 03/28/2013 loak 03/29/2013 zac 03/29/2013 this is my awk .. it gives me error cat employee_list | awk '(($2=='date... (3 Replies)
Discussion started by: Sara_84
3 Replies

7. Solaris

Python script not working in batch process

Good morning, I have a python 2.3 script that runs stand alone as intended when tested, then it was put into a ksh script. when running the ksh script it runs as intended. The problem is that my script does not run when the ksh script is called by another user who runs a batch process (as of right... (1 Reply)
Discussion started by: mhahe
1 Replies

8. Shell Programming and Scripting

string comparison not working inside while loop

The string comparison highlighted below is not working fine. Please help: while read line do # Get File name by deleting everything that preceedes and follows Filename as printed in cvs status' output f_name=`echo $line | sed -e 's/^File://' -e 's/ *Status:.*//' | awk '{print $NF}'` ... (4 Replies)
Discussion started by: sudvishw
4 Replies

9. Shell Programming and Scripting

String comparison not working inside while loop

Hi, In the code included below, the string comparision is not working fine. Please help while (( find_out >= i )) do file=`head -$i f.out|tail -1` dir=`dirname $file` cd $dir Status="" if ; then Status=`cvs -Q status... (3 Replies)
Discussion started by: sudvishw
3 Replies
Login or Register to Ask a Question