Reputation: 185
How to exit/close python program?
import csv
import os
with open('numbers.csv', 'w+', newline='') as csvfile:
fieldnames = ['name', 'lname']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
writer.writerow({'name': 'Alen', 'lname': 'Sow'})
os._exit(0)
I also tried:
import sys
sys.exit(0)
Upvotes: 0
Views: 2509
Reputation: 301
Your code should be like so:
import csv
with open('numbers.csv', 'w+', newline='') as csvfile:
fieldnames = ['name', 'lname']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
writer.writerow({'name': 'Alen', 'lname': 'Sow'})
exit() #or quit()
sys.exit(n)
exit the code with status n
without calling cleanup handlers.
os._exit(n)
should normally be used in the child process after a fork()
.
Upvotes: 1
Reputation: 79
To my knowledge, exit()
should work, although Aran-Fey is correct; it is usually not necessary.
Maybe you're getting Python mixed up with another language...
Upvotes: 0