akimebless
akimebless

Reputation: 33

Compare over directory and text file using Python

My goal is to compare two data one is from text file and one is from directory and after comparing it this is will notify or display in the console what are the data that is not found for example:

ls: /var/patchbundle/rpms/:squid-2.6.STABLE21-7.el5_10.x86_64.rpm NOT FOUND!
ls: /var/patchbundle/rpms/:tzdata-2014j-1.el5.x86_64.rpm 
ls: /var/patchbundle/rpms/:tzdata-java-2014j-1.el5.x86_64.rpm 
ls: /var/patchbundle/rpms/:wireshark-1.0.15-7.el5_11.x86_64.rpm
ls: /var/patchbundle/rpms/:wireshark-gnome-1.0.15-7.el5_11.x86_64.rpm 
ls: /var/patchbundle/rpms/:yum-updatesd-0.9-6.el5_10.noarch.rpm NOT FOUND

It must be like that. So Here's my python code.

import package, sys, os, subprocess

path = '/var/tools/tools/newrpms.txt'

newrpms = open(path, "r")
fds = newrpms.readline()
def checkrc(rc):
            if(rc != 0):
                    sys.exit(rc)
cmd = package.Errata()
for i in newrpms:
    rc = cmd.execute("ls /var/patchbundle/rpms/ | grep %newrpms ")
        if ( != 0):
                    cmd.logprint ("%s not found !" % i)
checkrc(rc)
sys.exit(0)
newrpms.close

Please see the shell script. This script its executing file but because I want to use another language that's why Im trying python

retval=0
for i in $(cat /var/tools/tools/newrpms.txt)
do
        ls /var/patchbundle/rpms/ | grep $i
        if [ $? != 0 ]
        then
                echo "$i NOT FOUND!"
                retval=255
        fi
done
exit $retval

Please see my Python code. What is wrong because it is not executing like the shell executing it.

Upvotes: 0

Views: 74

Answers (1)

TessellatingHeckler
TessellatingHeckler

Reputation: 28983

You don't say what the content of "newrpms.txt" is; you say the script is not executing how you want - but you don't say what it is doing; I don't know what package or package.Errata are, so I'm playing guess-the-problem; but lots of things are wrong.

  1. if ( != 0): is a syntax error. If {empty space} is not equal to zero?

  2. cmd.execute("ls /var/patchbundle/rpms/ | grep %newrpms ") is probably not doing what you want. You can't put a variable in a string in Python like that, and if you could newrpms is the file handle not the current line. That should probably be ...grep %s" % (i,)) ?

  3. The control flow is doing:

    1. Look in this folder, try to find files
    2. Call checkrc()
    3. Only quit with an error if the last file was not found
  4. newrpms.close isn't doing anything, it would need to be newrpms.close() to call the close method.

You're writing shell-script-in-Python. How about:

import os, sys

retval=0

for line in open('/var/tools/tools/newrpms.txt'):
    rpm_path = '/var/patchbundle/rpms/' + line.strip()
    if not os.path.exists(rpm_path):
        print rpm_path, "NOT FOUND"
        retval = 255
    else:
        print rpm_path

sys.exit(retval)

Edited code slightly, and an explanation:

The code is almost a direct copy of the shell script into Python. It loops over every line in the text file, and calls line.strip() to get rid of the newline character at the end. It builds rpm_path which will be something like "/var/patchbundle/rpms/:tzdata-2014j-1.el5.x86_64.rpm".

Then it uses sys.path.exists() which tests if a file exists and returns True if it does, False if it does not, and uses that test to set the error value and print the results like the shell script prints them. This replaces the "ls ... | grep " part of your code for checking if a file exists.

Upvotes: 1

Related Questions