0

I'm new of Python and i'm stuck on this. I need to check if i can find a string in a file. I have created the file ContEAN.py

the code:

import sys

for arg in sys.argv:
inputEAN=arg
EAN = open("/home/master/Documenti/Progetti/eanFZ.txt","r")

riga=EAN.readlines()

EAN.close()

print inputEAN

for i in range(len(riga)):
if inputEAN == riga[i]:
    print "OK"

From Command line:

$ python ContEAN.py 455

where "455" will be the value of inputEAN

and this is the content of eanFZ.txt:

7777
777
0000000000156
0000000015
455
9999
85485
656565

the problem is that i never have an ok as ultimate result and i don't know why...

  • 4
    Fix your indentation (I think that copy-pasting have eaten something, but it is very important in python) and learn how to debug. For starters I would print out all the relevant values, so you can see if you are comparing what you think you are. My bet is you don't, not sure though. – luk32 Feb 23 '15 at 23:11
  • [This](http://ericlippert.com/2014/03/05/how-to-debug-small-programs/) is a great primer on how to debug small programs. – aruisdante Feb 23 '15 at 23:12
  • possible duplicate of [Grep and Python](http://stackoverflow.com/questions/1921894/grep-and-python) – aruisdante Feb 23 '15 at 23:16
  • Could you please give use the error you are getting? Or is it giving you an error? – IronManMark20 Feb 23 '15 at 23:19
  • Remember to skip `sys.argv[0]` also strip newline from the lines of your file before comparing. – Paul Rooney Feb 23 '15 at 23:23

3 Answers3

1

First of all, the readlines() method returns an iterable object which can be iterated over in the loop. Each iteration will use the next line of the file.

Secondly, the lines of the file have a hidden newline character at the end, so it is not 'equal' to the string from argv. This can be removed with the rstrip() method.

for i in riga:
    if inputEAN == i.rstrip():
        print "OK"

(You may also want to check indentation. You need to go to a new indentation level after each for line and each if line. I have assumed this was due to copy and paste.)

EvenAdam
  • 313
  • 1
  • 2
  • 7
0

You could change your script as follows:

import sys

if len(sys.argv) == 1:
    exit;

inputEAN=sys.argv[1]

with open("file.txt","r") as f:
    for i, a_line in enumerate(f, 1):
        if inputEAN in a_line:
            print(i, a_line.strip())

This prints line number and the line where the given string is find.

Marcin
  • 215,873
  • 14
  • 235
  • 294
0

You could do it like this

import sys

if len(sys.argv) < 2:
    print "must provide args"
    sys.exit()

with open("eanFZ.txt","r") as f:
    lines=[line.strip() for line in f]
    args = sys.argv[1:]

    for line in lines:
        if line in args:
            print "OK"

Note the important things are

args = sys.argv[1:]

Dont use your sys.argv[0] in the comparison loop. Its the name of the script.

lines=[line.strip() for line in f]

Strip the newlines off the lines from your file, so the strings match.

Paul Rooney
  • 20,879
  • 9
  • 40
  • 61