1

I am writing a function that is reading an already processed file (file_name). In the processed file (open_file1) all lines are tuples. My problem is in this: in order to use the program, I have to always start with the command input as input file name. If the user enters TEAM IDENTIFIER i.e. the third elif statement without entering the input file name, then the program crashes. So, what I did was that in the third elif statement I checked the existence of the input file with an os statement. If the input file did not exist, I wrote an else statement to ask for another command (i.e. to input the input file name and start allover again). But, for some reason, when I am entering command as TEAM IDENTIFIER, the program is not working on the else statement of the third elif statement. Any suggestions? Thanks in advance.

 def main():
    command=input('Input cmd: ')
    while command.lower() !='quit':
        if command.lower()=='help':
            print("QUIT\n"\
                  "HELP\n"\
                  "INPUT filename\n"\
                  "TEAM identifier\n"\
                  "REPORT n HITS\n"\
                  "REPORT n BATTING\n"\
                  "REPORT n SLUGGING\n")
        elif command.startswith('INPUT') and len(command)>6:
            file_name=str(command[6:])
            open_file1=new_list(file_name)
            print(open_file1)

        elif command.startswith('TEAM') and len(command)>5:
            if os.path.isfile(open_file1):
                team_name=str(command[4:])
                for line in open_file1:
                    print(line[1])

            else:
                command=input('Input cmd: ')

        command=input('Input cmd: ')
main()

ERROR:

Traceback (most recent call last):
  File "C:\Users\Dasinator\Documents\Books IX\Python Examples\textbook examples\project 07\openfile.py", line 98, in <module>
    main()
  File "C:\Users\Dasinator\Documents\Books IX\Python Examples\textbook examples\project   07\openfile.py", line 81, in main
    if os.path.isfile(open_file1):
UnboundLocalError: local variable 'open_file1' referenced before assignment
  • you declare open_file1 in the first elif, so if your command starts with 'TEAM' it never gets defined. – skamsie Mar 15 '14 at 17:44
  • Yes, I understand that problem. Now the question is how can I move forward. If the file does not exist, then how can I go back to the main while loop? I don't even think the use of os statement is a correct one. All I need is how to check whether the input file exists in the third elif statement. –  Mar 15 '14 at 17:49

1 Answers1

0

The problem is in your line os.path.isfile(open_file1). The isfile function expects a string, but got a file object instead (if open_file1 exists), or a non-referenced variable (if open_file1 does not exist).

I would replace the if-else statement with a try-except block:

elif command.startswith('TEAM') and len(command)>5:
            try:
                team_name=str(command[4:])
                for line in open_file1:
                    print(line[1])

            except UnboundLocalError:
                command=input('Input cmd: ')
leeladam
  • 1,748
  • 10
  • 15
  • Thanks for the suggestion. However, I am trying to avoid a try-exception statement. So, I am trying to find a way to check the existence of the input_file, in case the user inputs the team identifier first. I came to understand that the os route was not the right one. Is there any other way to check the existence of a file, without initiating it first? –  Mar 15 '14 at 17:52
  • 1
    If I understand well, you want to check if the `open_file1` variable is initialized or not. This problem has several answers [here](http://stackoverflow.com/questions/843277/how-do-i-check-if-a-variable-exists-in-python). – leeladam Mar 15 '14 at 17:55
  • Tried other ways to avoid try-except statement, but at this point I think that is the only choice that seems to be working. Thanks –  Mar 15 '14 at 18:56