1 Introduction
When I was learning Baidu Fei Paddle course today, I saw a code that was very strange, it was like this,
See this code: with open("dataset/test_list.txt", "r") as f
It feels strange, it feels like I have never seen such code;
2 Use with open... as f to open the file
Since IOError may occur during file reading and writing, once an error occurs, the following f.close() will not be called;
Here we can use "with open ... as f" to ensure that the file can be closed correctly regardless of whether there is an error (similar to the role of try-finally);