HTML Diff
0 added 0 removed
Original 2026-01-01
Modified 2026-02-21
1 <p>In this lesson, you will learn how to do the most basic operations - opening and closing of files.</p>
1 <p>In this lesson, you will learn how to do the most basic operations - opening and closing of files.</p>
2 <h2>How to open files</h2>
2 <h2>How to open files</h2>
3 <p>In Python, we open a file using the open function, which requires the file path and the mode as parameters. The path is straightforward. We use the mode to specify how we want to use the file:</p>
3 <p>In Python, we open a file using the open function, which requires the file path and the mode as parameters. The path is straightforward. We use the mode to specify how we want to use the file:</p>
4 <ul><li>To write or read</li>
4 <ul><li>To write or read</li>
5 <li>To work with text or binary data</li>
5 <li>To work with text or binary data</li>
6 <li>To clear the file before writing to it</li>
6 <li>To clear the file before writing to it</li>
7 </ul><p>Now, we will work with files in two modes - for reading and writing text. To read something from a file, we first need to create it and write something into it. Let's start with that. We will open a file for writing:</p>
7 </ul><p>Now, we will work with files in two modes - for reading and writing text. To read something from a file, we first need to create it and write something into it. Let's start with that. We will open a file for writing:</p>
8 <p>The variable f now refers to a file object. Don't be alarmed by the type name. For now, you can pay attention only to the parameters:</p>
8 <p>The variable f now refers to a file object. Don't be alarmed by the type name. For now, you can pay attention only to the parameters:</p>
9 <ul><li>name specified when calling open</li>
9 <ul><li>name specified when calling open</li>
10 <li>mode specified when calling open</li>
10 <li>mode specified when calling open</li>
11 <li>encoding set to the default<a>UTF-8</a>, which is what you will mostly need</li>
11 <li>encoding set to the default<a>UTF-8</a>, which is what you will mostly need</li>
12 </ul><p>So, we have opened the file. Let us try to close it:</p>
12 </ul><p>So, we have opened the file. Let us try to close it:</p>
13 <p>Here, closed is an attribute of the f object. Attributes are variables within the associated object.</p>
13 <p>Here, closed is an attribute of the f object. Attributes are variables within the associated object.</p>
14 <h2>How to close files</h2>
14 <h2>How to close files</h2>
15 <p>Python is a language with automatic memory management. Knowing this, you can guess that the execution environment will close the file when we delete the last reference to the file object. It's easy.</p>
15 <p>Python is a language with automatic memory management. Knowing this, you can guess that the execution environment will close the file when we delete the last reference to the file object. It's easy.</p>
16 <p>In simple cases, we do not have to close files manually because scripts usually run briefly and don't open many files. Therefore, closing files upon script completion is generally acceptable. However, longer programs that run for extended periods may need to ensure that files are closed on time, preferably immediately after you read or write the required data to or from them.</p>
16 <p>In simple cases, we do not have to close files manually because scripts usually run briefly and don't open many files. Therefore, closing files upon script completion is generally acceptable. However, longer programs that run for extended periods may need to ensure that files are closed on time, preferably immediately after you read or write the required data to or from them.</p>