I have a script I have written to scan a directory of text files and if it finds them, it creates a system call to run the script on the txt file. I am still working on a couple little bugs that causes some of my system calls to fail. I would like, however, for this NOT to kill my script. I would just like to be informed of the error then move on with my life. It seems any successful call returns 0 while any call that resulted in error returns n. I tried to compare the result with 0 but it never gets that far. Any suggestions on how I can accomplish this??
import sys, getopt, os
def main(argv):
def scan_dir(path):
temp_path = path
print temp_path
for file in os.listdir(path):
temp_path += file
if file.endswith(".txt"):
result = os.system("python callscript.py -i %s" % path)
if result != 0
print "Error!"
temp_path = path
def usage():
print "usage: dostuff.py [hi:]\n \
\t -h\t print usage\n \
\t -i\t directory path\n"
sys.exit(2)
if(len(argv) == 0):
usage()
path = ''
try:
opts, args = getopt.getopt(argv,"hi:",["path="])
except getopt.GetoptError:
usage()
for opt, arg in opts:
if opt == '-h':
usage()
elif opt in ("-i", "--ipath"):
path = arg
if path.endswith('/') == False:
path += '/'
scan_dir(path)
if __name__ == "__main__":
main(sys.argv[1:])