Try...finally in Python ... The understanding

Source: Internet
Author: User

Recently in the follow Mooc learning Python, as a novice will encounter a lot of problems, such as the file read and write when the file is open to normal or not close the problem of the use of try...finally ..., after looking for information, it is understood, then the small part to talk about their views:

===============================

= = = Try...finally ... Use format:

Try

f = open (' Name.txt ', ' R ')

While True:

Program...

Finally

F.close ()

===============================

= = = below to see a specific code of operation

Try:

f = open (' Names.txt ')

While True:

line = F.read ()

If Len (line) ==0:

Break

Print Line

Finally

F.close ()

===============================

Check the information on the Internet when you see the Time.sleep (t) module, the main role is to delay the T-second output, of course, you need to add the import time statement at the beginning of the program, try...finally ... Is the content of the exception handling file, which ensures that the file will be close regardless of whether it is opened normally.

===============================

Explanation of the not in place want everyone to explore

Try...finally in Python ... The understanding

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.