python - comparing outputs from command.getoutput(cmd) -


i have following code working apart 1 problem. im trying compare 2 outputs commands.getoutput(cmd) im getting syntax errors. understand inherently wrong, point im doing wrong

def diffgenerator():    try: sys.argv[1]   except: print "no directory scan defined\n"    try: sys.argv[2]   except: print "no delay time defined\n"    scandirectory = sys.argv[1]   if not os.path.exists(scandirectory):     print "scan directory not exist :" + scandirectory     cmd = "ls -l " + scandirectory    try:     difffilea = commands.getoutput(cmd)     print "printing difffilea" + difffilea     time.sleep(1)     difffileb = commands.getoutput(cmd)     if operator.ne(difffilea, difffileb)       print "new file placed within " + scandirectory   except:     print "blah" 

you might want consider subprocess

http://docs.python.org/library/subprocess.html

subprocess.popen(['ls','-a'], stdout = subprocess.pipe, stdin = subprocess.pipe) results = subprocess.communicate()[0] #where [0] stdout , [1] stderr 

edit:

also can please expand out try except loops , use specific exceptions e.g.

bad:

try: sys.argv[1] except: print "no directory scan defined\n" 

good:

try:     sys.argv[1] except indexerror:     print "no directory scan defined\n" 

Comments

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -