1
00:00:05,120 --> 00:00:08,612
For this video, we'll continue 
with our read_poem code.  | www.downloadly.ir

2
00:00:08,800 --> 00:00:15,361
Before we make any changes, have a look at line 7.
This closes the file, and is very important. 

3
00:00:15,520 --> 00:00:19,620
If you don't close a file when you're finished 
with it, you could run out of file handles. 

4
00:00:21,040 --> 00:00:26,455
File handles (or file descriptors, on Mac 
and Linux) are used by the operating system, 

5
00:00:26,455 --> 00:00:31,280
to keep track of the files that have been opened.
They're system-wide, which means the available 

6
00:00:31,280 --> 00:00:34,639
file handles are shared by every 
process running on your computer. 

7
00:00:34,720 --> 00:00:38,800
Although running out of file handles is harder 
to do these days, it's still possible with a 

8
00:00:38,800 --> 00:00:41,886
long-running process that opens a lot of files.

9
00:00:44,051 --> 00:00:50,009
Another consequence of failing to close a file, when
writing data to it, is that data might be lost. 

10
00:00:50,130 --> 00:00:55,175
Neither of these situations are good, so it's 
very important to close every file that you open. 

11
00:00:55,360 --> 00:00:59,749
Python makes this easy – it can 
close files automatically for us. 

12
00:01:01,120 --> 00:01:04,160
Back in IntelliJ, I'll 
comment out our existing code, 

13
00:01:04,160 --> 00:01:06,820
and do the same thing in a more Pythonic way:

14
00:01:32,611 --> 00:01:36,000
Line 9 uses the open function, just like before.

15
00:01:36,160 --> 00:01:40,400
But we use a with statement.
Because with is a statement, not a function, 

16
00:01:40,400 --> 00:01:47,016
we don't get a file object returned. Instead, we 
use "as jabber" to give a name to the file object. 

17
00:01:47,120 --> 00:01:50,804
Run the program:
to make sure we still get the same output. 

18
00:01:52,960 --> 00:01:56,400
Opening a file this way is preferred, 
because the file will be automatically 

19
00:01:56,400 --> 00:02:01,040
closed when the with block terminates. There's 
no chance that we'll forget to close the file, 

20
00:02:01,040 --> 00:02:04,048
and this is how I'll open 
all our files from now on. 

21
00:02:04,048 --> 00:02:09,360
This makes our code robust, and avoids 
resource leaks – such as keeping files open. 

22
00:02:09,360 --> 00:02:13,931
I'll switch to my browser, and check 
out the Resource Leak page on Wikipedia. 

23
00:02:16,000 --> 00:02:20,160
I try to explain all the terms that are used 
in this course, but if I use a term that you're 

24
00:02:20,160 --> 00:02:24,960
not familiar with, google it. Everything 
we need to know is out there, somewhere. 

25
00:02:24,960 --> 00:02:29,600
This article's quite short. A resource leak 
occurs when a program fails to release some 

26
00:02:29,600 --> 00:02:32,880
resource that it's acquired.
That could be a file handle, 

27
00:02:32,880 --> 00:02:37,840
or it could be memory (amongst other things).
We don't have to worry too much about freeing 

28
00:02:37,840 --> 00:02:41,745
up memory, because Python deals 
with memory management for us. 

29
00:02:41,840 --> 00:02:45,920
If we use with to open any resource that 
supports it, Python handles managing those 

30
00:02:45,920 --> 00:02:50,240
resources for us, as well.
I'll finish this video here. 

31
00:02:50,240 --> 00:02:53,840
Have a read through this article 
– the Causes section is interesting, 

32
00:02:53,840 --> 00:02:58,080
and starts by saying that "Resource leaks 
are generally due to programming errors". 

33
00:02:58,080 --> 00:03:01,166
Opening our files using with 
makes sure the files are closed, 

34
00:03:01,166 --> 00:03:05,920
and prevents one programming error we might make.
In the next video, we'll look at different ways 

35
00:03:05,920 --> 00:03:11,040
to read from our file, now that we know 
how to open it. See you in the next video. | www.downloadly.ir

