Có chức năng kết thúc chương trình trong Python không?

Sau đó, tôi đi vào phần tiếp tục ngay lập tức của vòng lặp bên ngoài, và trước khi bất kỳ điều gì khác được mã thực thi, tôi viết

    if immediateExit:
        sys.exit['CSV file corrupted 1.']

Tùy thuộc vào mức độ phức tạp, đôi khi câu lệnh trên cũng cần được lặp lại trong các phần ngoại trừ, v.v.

    if immediateExit:
        sys.exit['CSV file corrupted 1.5.']

Thông báo tùy chỉnh cũng dành cho việc gỡ lỗi cá nhân của tôi, vì các số có cùng mục đích - để xem tập lệnh thực sự thoát ra ở đâu

'CSV file corrupted 1.5.'

Trong trường hợp cụ thể của tôi, tôi đang xử lý tệp CSV mà tôi không muốn phần mềm chạm vào, nếu phần mềm phát hiện ra thì tệp đó bị hỏng. Do đó, đối với tôi, điều rất quan trọng là thoát khỏi toàn bộ tập lệnh Python ngay sau khi phát hiện ra lỗi có thể xảy ra

Các hàm quit[], exit[], sys. thoát [] và hệ điều hành. _exit[] có chức năng gần như giống nhau khi chúng tăng ngoại lệ SystemExit theo đó trình thông dịch Python thoát và không có dấu vết ngăn xếp nào được in. Chúng tôi có thể bắt ngoại lệ để chặn các lần thoát sớm và thực hiện các hoạt động dọn dẹp; .  

hàm thoát [] trong Python

Hàm bỏ [] chỉ hoạt động nếu mô-đun trang được nhập, vì vậy không nên sử dụng hàm này trong mã sản xuất. Mã sản xuất có nghĩa là mã đang được sử dụng bởi đối tượng dự định trong tình huống thực tế. Chức năng này chỉ nên được sử dụng trong trình thông dịch. Nó làm tăng ngoại lệ SystemExit đằng sau hậu trường. Nếu bạn in nó, nó sẽ đưa ra một thông báo

Thí dụ.  

Python3




# Python program to demonstrate

# quit[]

for iin range3_______0

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
1
0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
2

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
3

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
3_______3_______5

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
3_______3_______7

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
3_______3_______9

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
3_______14_______1 i
An exception has occurred, use %tb to see the full traceback.

SystemExit: Age less than 18
3
An exception has occurred, use %tb to see the full traceback.

SystemExit: Age less than 18
3
An exception has occurred, use %tb to see the full traceback.

SystemExit: Age less than 18
5
An exception has occurred, use %tb to see the full traceback.

SystemExit: Age less than 18
6

An exception has occurred, use %tb to see the full traceback.

SystemExit: Age less than 18
7

An exception has occurred, use %tb to see the full traceback.

SystemExit: Age less than 18
7_______14_______9

An exception has occurred, use %tb to see the full traceback.

SystemExit: Age less than 18
7_______23_______1
In child process
Process ID: 25491
Hello ! Geeks
Child exiting..

In parent process
Child's exit code: 0
2

An exception has occurred, use %tb to see the full traceback.

SystemExit: Age less than 18
7_______23_______4

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
3_______23_______1
In child process
Process ID: 25491
Hello ! Geeks
Child exiting..

In parent process
Child's exit code: 0
7

đầu ra

0
1
2
3
4
Use quit[] or Ctrl-D [i.e. EOF] to exit

hàm thoát [] trong Python

Lối ra [] được xác định trong trang web. py và nó chỉ hoạt động nếu mô-đun trang web được nhập, vì vậy nó chỉ được sử dụng trong trình thông dịch. Nó giống như một từ đồng nghĩa với từ bỏ [] để làm cho Python thân thiện hơn với người dùng. Nó cũng đưa ra một thông điệp khi được in.  

Thí dụ

Python3




# Python program to demonstrate

In child process
Process ID: 25491
Hello ! Geeks
Child exiting..

In parent process
Child's exit code: 0
9

for iin range3_______0

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
1
0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
2

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
3_______3_______5

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
3_______3_______7

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
3_______166_______2

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
3_______14_______1 i
An exception has occurred, use %tb to see the full traceback.

SystemExit: Age less than 18
3
An exception has occurred, use %tb to see the full traceback.

SystemExit: Age less than 18
3
An exception has occurred, use %tb to see the full traceback.

SystemExit: Age less than 18
5
An exception has occurred, use %tb to see the full traceback.

SystemExit: Age less than 18
6

 

An exception has occurred, use %tb to see the full traceback.

SystemExit: Age less than 18
7_______167_______1

An exception has occurred, use %tb to see the full traceback.

SystemExit: Age less than 18
7_______23_______1for4

An exception has occurred, use %tb to see the full traceback.

SystemExit: Age less than 18
7_______167_______6

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
3_______23_______1
In child process
Process ID: 25491
Hello ! Geeks
Child exiting..

In parent process
Child's exit code: 0
7

đầu ra

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit

hệ thống. thoát [[arg]] bằng Python

Không giống như thoát[] và thoát[], sys. exit[] được coi là tốt để sử dụng trong mã sản xuất cho mô-đun sys luôn có sẵn. Đối số tùy chọn arg có thể là một số nguyên đưa ra lối ra hoặc một loại đối tượng khác. Nếu là số nguyên, số 0 được coi là “kết thúc thành công”.  
Ghi chú. Một chuỗi cũng có thể được chuyển đến sys. phương thức thoát [].  

Thí dụ.  

Python3




# Python program to demonstrate

i1

i2 i3

 

i4

An exception has occurred, use %tb to see the full traceback.

SystemExit: Age less than 18
3 i6

 

i7

An exception has occurred, use %tb to see the full traceback.

SystemExit: Age less than 18
1 i9in0in1

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
3_______169_______3

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
3_______169_______5in6in7

in8

An exception has occurred, use %tb to see the full traceback.

SystemExit: Age less than 18
6

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
3_______23_______1
0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
0range3range4

đầu ra

An exception has occurred, use %tb to see the full traceback.

SystemExit: Age less than 18

hệ điều hành. _exit[n] trong Python

hệ điều hành. Phương thức _exit[] trong Python được sử dụng để thoát khỏi quy trình với trạng thái đã chỉ định mà không gọi trình xử lý dọn dẹp, xóa bộ đệm stdio, v.v.  

Ghi chú. Phương pháp này thường được sử dụng trong tiến trình con sau os. lệnh gọi hệ thống fork[]. Cách tiêu chuẩn để thoát khỏi quy trình là sys. phương thức thoát [n]

Python3




range5

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
3

range7

i2 range9

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
3

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
01

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
02

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
03_______14_______3
0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
05

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
3

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
3

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
08

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
09

An exception has occurred, use %tb to see the full traceback.

SystemExit: Age less than 18
1
0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
11_______3_______12
An exception has occurred, use %tb to see the full traceback.

SystemExit: Age less than 18
6

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
3

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
3_______23_______1
0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
0
0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
18range4

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
3_______3_______21

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
3_______3_______23

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
3_______3_______25

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
3_______3_______27

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
3_______3_______29

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
3_______3_______31
An exception has occurred, use %tb to see the full traceback.

SystemExit: Age less than 18
3
0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
33
0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
12range4

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
3

An exception has occurred, use %tb to see the full traceback.

SystemExit: Age less than 18
7

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
3_______3_______39

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
3_______3_______41

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
3_______3_______43

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
3_______3_______45

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
3

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
3_______3_______48

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
3_______3_______50

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
3_______3_______52

An exception has occurred, use %tb to see the full traceback.

SystemExit: Age less than 18
7

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
3_______3_______55

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
3_______3_______57

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
3_______14_______1
0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
60
0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
61_______3_______62

An exception has occurred, use %tb to see the full traceback.

SystemExit: Age less than 18
7_______3_______64
An exception has occurred, use %tb to see the full traceback.

SystemExit: Age less than 18
3
0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
66
0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
61
0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
68

An exception has occurred, use %tb to see the full traceback.

SystemExit: Age less than 18
7_______23_______1
0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
0
0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
72
0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
73

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
3

in8

An exception has occurred, use %tb to see the full traceback.

SystemExit: Age less than 18
6

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
3_______23_______1
0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
0
0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
80_______170_______4

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
3_______23_______1
0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
0
0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
85_______3_______86

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
3_______23_______1
0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
0_______3_______90_______170_______4

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
3_______23_______1
0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
0
0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
95_______170_______4

An exception has occurred, use %tb to see the full traceback.

SystemExit: Age less than 18
7

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
3_______3_______99

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
3_______14_______01

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
3_______14_______03

0
1
2
3
4
Use exit[] or Ctrl-D [i.e. EOF] to exit
3_______14_______05

đầu ra

In child process
Process ID: 25491
Hello ! Geeks
Child exiting..

In parent process
Child's exit code: 0

Phần kết luận. Trong số bốn chức năng thoát trên, sys. exit[] được ưu tiên chủ yếu vì các hàm exit[] và thoát[] không thể được sử dụng trong mã sản xuất trong khi hệ điều hành. _exit[] chỉ dành cho các trường hợp đặc biệt khi cần thoát ngay lập tức

Có cách nào để kết thúc chương trình bằng Python không?

Ctrl + C trên Windows có thể được sử dụng để chấm dứt tập lệnh Python và Ctrl + Z trên Unix sẽ tạm dừng [đóng băng] việc thực thi tập lệnh Python. Nếu bạn nhấn CTRL + C trong khi tập lệnh đang chạy trong bảng điều khiển, tập lệnh sẽ kết thúc và phát sinh ngoại lệ.

Hàm nào dừng chương trình trong Python?

Để dừng thực thi mã trong Python, trước tiên bạn cần nhập đối tượng sys. Sau đó, bạn có thể gọi phương thức exit[] để dừng chạy chương trình. Đây là cách dừng thực thi mã đa nền tảng, đáng tin cậy nhất. Đây là một ví dụ đơn giản.

Chủ Đề