comparing outputs from command.getoutput(cmd)
I have the following code that is working apart from one problem. Im trying to compare the two outputs from commands.getoutput(cmd) but im getting syntax errors. I understand that this is probably inherently wrong, but could someone point what im doing wrong
def DiffGenerator():
try: sys.argv[1]
except: print "No Directory to 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 does 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(D开发者_开发知识库iffFileA, DiffFileB)
print "New File placed within " + ScanDirectory
except:
print "BLAH"
You might want to 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] is the stdout and [1] is the stderr
EDIT:
Also can you please expand out your try except loops and use specific exceptions e.g.
Bad:
try: sys.argv[1]
except: print "No Directory to scan defined\n"
Good:
try:
sys.argv[1]
except IndexError:
print "No directory to scan defined\n"
The line if operator.ne(DiffFileA, DiffFileB)
is missing a colon, which could explain a SyntaxError. By the way, when reporting an error please copy and paste exactly the error message that you see.
But using if operator.ne(A,B): is a very unpythonic way to write if A != B:, and I'd avoid it.
精彩评论