Python Forum
python3 does not understand: exit(0)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
python3 does not understand: exit(0)
#1
my question here Hi all. If I like to stop the program, what shall I use instead of :  exit(0) ??

My code here:
# file3.p
# import sys

rep = 'y'
while (rep != 'n'):
    name = input('Enter the name of the file:')
    print("You entered: ", name)
    name = '/home/sylvain/' + name
    print("name is: ", name)
    try:
        fh = open(name, "r+")
    except IOError:
    #except ioerror:
        print("Cannot find file ")
    else:
        str = fh.read(10);
        print("Read String is : ", str)

# Check current position
        position = fh.tell();
        print("Current file position : ", position)

# Reposition pointer at the beginning once again
        position = fh.seek(0, 0);
        str = fh.read(140);
        print("Again read String is : ", str)
        fh.close()
        input('Try for another file ?  y/n ?')
        if rep=='n':
            exit(0)
        else:
            rep='y' 
Moderator Larz60+: Removed formatting (A- button). Please do so in the future.
Reply
#2
need to import sys,
then it's
import sys
sys.exit(0)
or
from sys import exit
exit(0)
Reply
#3
Sorry, none of your two proposals works.
Reply
#4
line 28
rep = input('Try for another file ?  y/n ?')
if rep == 'n':
    sys.exit(0)
# etc.
You have missed to asign the input to the variable
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#5
Other trouble now: .................................................................................................................................................................
File "/home/sylvain/PycharmProjects/py2/file3.py", line 29, in <module>
sys.exit(0)
NameError: name 'sys' is not defined

Process finished with exit code 1
Reply
#6
The sys  is a Python module. In order to use it put this at the beginning of the code. Under the shebang line.
import sys
Or uncomment line 2 cause you already have it.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#7
Yes M. Wavic. At the top of the file I must put import sys . Now it works well. Python easier than C++ ??
Reply
#8
For me, yes
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#9
Python programs end when they get to the end of the file.  Using sys.exit() is like running down the hall screaming that there's a fire, sure it works and will certainly stop the program, but it's also terribly poor style.

Consider someone else, who might look at your code.  At first glance, you've written an infinite loop.  But then, nested three levels deep, hidden behind user input and string comparisons, is a circuit breaker that busts out of the program.

If you're using sys.exit() to end the program, your program is probably written poorly.  In my opinion, the only valid use for it, is to emit a status code when the program ends.
Reply
#10
I agree with Nilamo. The whole if/else at the end of the program can be removed. If rep == 'n', then the loop will end, and the program will end. There is no need to force it with sys.exit.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  python difference between sys.exit and exit() mg24 1 1,764 Nov-12-2022, 01:37 PM
Last Post: deanhystad
  How to understand the byte notation in python3 blackknite 3 2,844 Feb-23-2021, 04:45 PM
Last Post: bowlofred
  Gnuradio python3 is not compatible python3 xmlrpc library How Can I Fix İt ? muratoznnnn 3 4,823 Nov-07-2019, 05:47 PM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020