Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to read files safely with Try finally and with statements of python

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces how to use python's Try finally sentence and with sentence to safely read the file related knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe you will have something to gain after reading this article on how to use python's Try finally sentence and with sentence to read the file safely, let's take a look together.

Try...finally statement

Enter:

#! / usr/bin/python

# Filename: finally.py

Import time

Try:

F = open ('poem.txt')

While True:

# our usual file-reading idiom

Line = f.readline ()

If len (line) = = 0:

Break

Print (line, end='')

Time.sleep (2)

# To make sure it runs for a while

Except KeyboardInterrupt:

Print ('! You cancelled the reading from the file.') finally:

F.close ()

Print ('(Cleaning up: Closed the file)')

Output:

$python finally.py

Programming is fun

When the work is done

If you wanna make your work also fun:

!! You cancelled the reading from the file.

(Cleaning up: Closed the file)

Explanation:

When we operate on the file, in order to ensure that there are abnormal events, we can close the file normally, so we use the try...finally function. When there is an exception, the exception is thrown and the file operation is closed.

In this example, first open the file operation, and then read the file, in order to extend the running time in try, we added sleep (2) pause for two seconds, during which we artificially interrupted through the forced Ctrl C operation, causing an exception, and entered the finally to normally perform the file closing operation.

With statement

Enter:

#! / usr/bin/python

# Filename: using_with.py

With open ("poem.txt") as f:

For line in f:

Print (line, end='')

Output:

$python using_with.py

Programming is fun

When the work is done

Explanation:

Like the function of the try...finally statement, the with function can also protect the normal operation of the file. When an exception is encountered, the file is closed.

This is the end of the article on "how to read files safely with python Try finally statements and with statements". Thank you for reading! I believe you all have a certain understanding of "how to read documents safely with python Try finally sentences and with sentences". If you want to learn more, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report