0

I wrote this statement in Python (for convert the program in an executable) in order to load all text files in a folder (*.txt) or a single text file, check if the data format is correct (get_parse and check_header) and process the data. If the data format is not correct the program ask to restart (doit = raw_input("Continue (Y/N)?[Y]: ") . In the first case (*.txt) when the data format is not correct (delimiter '.' and presence of Header in the first line) the program reports the error and restart (if Y is press) from INPUT = raw_input("Input (*.txt): "). In the second case (load a single text file) if the data format is not correct the program closes.

I wish to give also for the single load text file choose the possibility to the user to restart the program from INPUT = raw_input("Input (*.txt): )

while True:
    INPUT = raw_input("Input (*.txt): ")
    if os.path.split(INPUT)[1] == "*.txt":
        file_list = glob.glob(os.path.join(os.path.split(INPUT)[0], '*.txt'))
        for file in file_list:
            file_head, file_tail = os.path.split(file)
            file_root, file_suffix = os.path.splitext(file_tail)
            try:
                parse = get_parse(file)
            except Exception:
                print ValueError("%s has delimiter type not valid" % file_tail)
                raw_input("press any key to exit")
                print "Exiting..."
                break
            if check_header(file):
                print ValueError("%s has an Header" % file_tail)
                raw_input("press any key to exit")
                print "Exiting..."
                break
                # do other stuff
        doit = raw_input("Continue (Y/N)?[Y]: ")
        if doit == 'N'or doit == 'n':
            print "Exiting....."
            break
    else:
        try:
            parse = get_parse(INPUT)
        except Exception:
            print ValueError("Delimiter type not valid")
            raw_input("press any key to exit")
            print "Exiting..."
            break
        if check_header(INPUT):
            print ValueError("Header is not valid")
            raw_input("press any key to exit")
            print "Exiting..."
            break
        # do other stuff
        doit = raw_input("Continue (Y/N)?[Y]: ")
        if doit == 'N'or doit == 'n':
            print "Exiting....."
            break

in the second case, using the command prompt of windows is the text file is not correct

Input (*.txt): C:\test.txt
Delimiter type not valid
press any key to exit
Exiting.....

C:\Users\>

in the first case (*.txt)

Input (*.txt): C:\*.txt
Area1_1.txt has delimiter type not valid
press any key to exit
Exiting...
Continue (Y/N)?[Y]:
Gianni Spear
  • 7,033
  • 22
  • 82
  • 131

1 Answers1

1

Your question is still unclear, but I think you want to use continue instead of break, if your goal is to restart the loop after an error. Also, you should handle Exceptions differently:

<snip>
else:
    try:
        parse = get_parse(INPUT)
    except ValueError:
        print "Delimiter type not valid"
        print "Try again..."
        continue
    if check_header(INPUT):
        print "Header is not valid"
        print "Try again..."
        continue
    # do other stuff
    doit = raw_input("Continue (Y/N)?[Y]: ")
    if doit == 'N'or doit == 'n':
        print "Exiting....."
        break
Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561