0
file = input("Enter file name:")
try:
   fhand = open(file,'r')
except:
    print("File not found")
    quit() # Error: name 'quit' is not defined


count = 0
for line in fhand:
    line =line.strip()
    if line.startswith('Subject'):
       count+=1
print('There were,',count,'subject lines in ',file)

This error should not occur. I am confused to heights what am I doing wrong here. i get the error 'name 'quit' is not defined'. Which should not come.

4
  • I think you mean exit()? Commented May 10, 2020 at 5:42
  • @MoonCheesez quit(), exit(), sys.exit() and os._exit(), all should work. but none of them work. same error appears always. This is a very weird behavior. Commented May 10, 2020 at 5:44
  • @MoonCheesez I just checked it with Python IDE. The code runs flawlessly there. But with anaconda/Spyder this error surfaces. Commented May 10, 2020 at 5:59
  • Keep your posted code small. You do not need the second part of it to asl your question. The shorter is the fragment, the easier it is to spot the error. Commented May 10, 2020 at 7:36

1 Answer 1

1

quit() and exit() rely on the site module, and, to my knowledge, they're designed for being used in interactive mode and not in actual programs or production code.

Instead, I'd recommend making your program look more like this:

file = input("Enter file name:")
try:
    fhand = open(file,'r')
    count = 0
    for line in fhand:
        line =line.strip()
        if line.startswith('Subject'):
           count+=1
    fhand.close()
    print('There were,',count,'subject lines in ',file)
except FileNotFoundError:
    print("File not found")

You also may want to read the file immediately and close it sooner.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.