Grass
Grass

Reputation: 185

How to exit and close the python program

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)

The above script doesn't close/exit the python script.

I also tried:

import sys
sys.exit(0)

But this also didn't close/exit the python script.

Upvotes: 0

Views: 2509

Answers (2)

Muzol
Muzol

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

Anonymous
Anonymous

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

Related Questions