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
i
in
range
3_______0
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit1
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit2
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit3
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit3_______3_______5
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit3_______3_______7
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit3_______3_______9
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit3_______14_______1
i
An exception has occurred, use %tb to see the full traceback. SystemExit: Age less than 183
An exception has occurred, use %tb to see the full traceback. SystemExit: Age less than 183
An exception has occurred, use %tb to see the full traceback. SystemExit: Age less than 185
An exception has occurred, use %tb to see the full traceback. SystemExit: Age less than 186
An exception has occurred, use %tb to see the full traceback. SystemExit: Age less than 187
An exception has occurred, use %tb to see the full traceback. SystemExit: Age less than 187_______14_______9
An exception has occurred, use %tb to see the full traceback. SystemExit: Age less than 187_______23_______1
In child process Process ID: 25491 Hello ! Geeks Child exiting.. In parent process Child's exit code: 02
An exception has occurred, use %tb to see the full traceback. SystemExit: Age less than 187_______23_______4
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit3_______23_______1
In child process Process ID: 25491 Hello ! Geeks Child exiting.. In parent process Child's exit code: 07
đầ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: 09
for
i
in
range
3_______0
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit1
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit2
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit3_______3_______5
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit3_______3_______7
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit3_______166_______2
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit3_______14_______1
i
An exception has occurred, use %tb to see the full traceback. SystemExit: Age less than 183
An exception has occurred, use %tb to see the full traceback. SystemExit: Age less than 183
An exception has occurred, use %tb to see the full traceback. SystemExit: Age less than 185
An exception has occurred, use %tb to see the full traceback. SystemExit: Age less than 186
An exception has occurred, use %tb to see the full traceback. SystemExit: Age less than 187_______167_______1
An exception has occurred, use %tb to see the full traceback. SystemExit: Age less than 187_______23_______1
for
4An exception has occurred, use %tb to see the full traceback. SystemExit: Age less than 187_______167_______6
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit3_______23_______1
In child process Process ID: 25491 Hello ! Geeks Child exiting.. In parent process Child's exit code: 07
đầ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
i
1
i
2 i
3
i
4
An exception has occurred, use %tb to see the full traceback. SystemExit: Age less than 183
i
6
i
7
An exception has occurred, use %tb to see the full traceback. SystemExit: Age less than 181
i
9in
0in
10 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit3_______169_______3
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit3_______169_______5
in
6in
7in
8
An exception has occurred, use %tb to see the full traceback. SystemExit: Age less than 186
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit3_______23_______1
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit0
range
3range
4đầ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
range
5
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit3
range
7
i
2 range
9
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit3
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit01
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit02
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit03_______14_______3
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit05
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit3
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit3
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit08
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit09
An exception has occurred, use %tb to see the full traceback. SystemExit: Age less than 181
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit11_______3_______12
An exception has occurred, use %tb to see the full traceback. SystemExit: Age less than 186
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit3
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit3_______23_______1
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit0
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit18
range
40 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit3_______3_______21
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit3_______3_______23
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit3_______3_______25
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit3_______3_______27
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit3_______3_______29
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit3_______3_______31
An exception has occurred, use %tb to see the full traceback. SystemExit: Age less than 183
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit33
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit12
range
40 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit3
An exception has occurred, use %tb to see the full traceback. SystemExit: Age less than 187
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit3_______3_______39
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit3_______3_______41
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit3_______3_______43
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit3_______3_______45
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit3
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit3_______3_______48
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit3_______3_______50
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit3_______3_______52
An exception has occurred, use %tb to see the full traceback. SystemExit: Age less than 187
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit3_______3_______55
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit3_______3_______57
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit3_______14_______1
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit60
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit61_______3_______62
An exception has occurred, use %tb to see the full traceback. SystemExit: Age less than 187_______3_______64
An exception has occurred, use %tb to see the full traceback. SystemExit: Age less than 183
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit66
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit61
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit68
An exception has occurred, use %tb to see the full traceback. SystemExit: Age less than 187_______23_______1
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit0
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit72
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit73
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit3
in
8
An exception has occurred, use %tb to see the full traceback. SystemExit: Age less than 186
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit3_______23_______1
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit0
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit80_______170_______4
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit3_______23_______1
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit0
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit85_______3_______86
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit3_______23_______1
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit0_______3_______90_______170_______4
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit3_______23_______1
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit0
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit95_______170_______4
An exception has occurred, use %tb to see the full traceback. SystemExit: Age less than 187
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit3_______3_______99
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit3_______14_______01
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit3_______14_______03
0 1 2 3 4 Use exit[] or Ctrl-D [i.e. EOF] to exit3_______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