diff --git a/docs/source/book/07_files/index.rst b/docs/source/book/07_files/index.rst index 7fe6cf1..3394842 100644 --- a/docs/source/book/07_files/index.rst +++ b/docs/source/book/07_files/index.rst @@ -7,7 +7,7 @@ 7. Working with files ============================ -In real life, in order to make full use of everything considered before this section you need to understand how to work with files. +In real life, in order to make full use of everything covered before this section you need to understand how to work with files. When working with network equipment (and not only), files can be: diff --git a/docs/source/book/07_files/open.rst b/docs/source/book/07_files/open.rst index d639e9c..5860b7c 100644 --- a/docs/source/book/07_files/open.rst +++ b/docs/source/book/07_files/open.rst @@ -30,6 +30,7 @@ File opening modes: * if file does not exist, a new one is created * ``w+`` - open file for reading and writing + * if file exists, its content is removed * if file does not exist, a new one is created @@ -37,4 +38,5 @@ File opening modes: * ``a+`` - open file for reading and writing. Data is added to the end of file .. note:: + r - read; a - append; w - write diff --git a/docs/source/book/07_files/read.rst b/docs/source/book/07_files/read.rst index d44b60d..40ff33d 100644 --- a/docs/source/book/07_files/read.rst +++ b/docs/source/book/07_files/read.rst @@ -25,9 +25,7 @@ Let’s see how to read contents of files using the example of r1.txt: ``read()`` ^^^^^^^^^^ -Method ``read()`` reads the entire file to one string. - -Example of the use of ``read()``: +Method ``read()`` reads the entire file to one string: .. code:: python @@ -39,7 +37,10 @@ Example of the use of ``read()``: In [3]: f.read() Out[3]: '' -When reading a file once again an empty line is displayed in line 3. This is because the whole file is read when ``read()`` method is called. And after the file has been read the cursor stays at the end of file. The cursor position can be controlled by means of ``seek()`` method. +When reading a file once again an empty line is displayed in line 3. +This is because the whole file is read when ``read()`` method is called. +And after the file has been read the cursor stays at the end of file. +The cursor position can be controlled by means of ``seek()`` method. ``readline()`` ^^^^^^^^^^^^^^ diff --git a/docs/source/book/07_files/with.rst b/docs/source/book/07_files/with.rst index fe387ef..3a3fdf9 100644 --- a/docs/source/book/07_files/with.rst +++ b/docs/source/book/07_files/with.rst @@ -1,7 +1,7 @@ -Construction with +with statement ---------------- -Construction **with** is a context manager. +The ``with`` statement is called the context manager. Python has a more convenient way of working with files than the ones used so far - construction ``with``: @@ -42,7 +42,7 @@ Pay attention to how lines of the file are read: When file needs to be run line by line, it is best to use this option. -In previous output there were extra empty lines between lines of the file because **print** adds another line feed character. +In previous output there were extra empty lines between lines of the file because ``print`` adds another line feed character. To get rid of this you can use ``rstrip`` method: @@ -89,7 +89,7 @@ Open two files Sometimes you have to work with two files simultaneously. For example, write some lines from one file to another. -In this case you can open two files in **with** block as follows: +In this case you can open two files in ``with`` block as follows: .. code:: python diff --git a/docs/source/book/07_files/write.rst b/docs/source/book/07_files/write.rst index 4ad90a8..a3e66c5 100644 --- a/docs/source/book/07_files/write.rst +++ b/docs/source/book/07_files/write.rst @@ -1,14 +1,14 @@ File writing ------------- -When writing it is very important to decide how to open a file in order not to accidentally delete it: +When writing information to a file, it is very important to specify the correct mode for opening the file, so as not to accidentally delete it: * ``w`` - open file for writing. If file exists, its content is removed -* ``a`` - open file to add data. Data is added to the end of the file +* ``a`` - open file to add data. Data is appended to the end of the file Both modes create a file if it does not exist. -These methods are used to write to a file: +The following methods are used to write to a file: * ``write()`` - write one line to file * ``writelines()`` - allows to send as argument a list of strings @@ -113,15 +113,14 @@ Writing cfg_lines list into the file: !service timestamps debug datetime msec localtime show-timezone yearservice timestamps log datetime msec localtime show-timezone yearservice password-encryptionservice sequence-numbers!no ip domain lookup!ip ssh version 2! As a result, all lines in the list were written into one line because there was no symbol ``\n`` at the end of lines. - -You can add line feed character in different ways. For example, you can simply process list in the loop: +You can add newline character in different ways. For example, you can loop through a list: .. code:: python In [13]: cfg_lines2 = [] In [14]: for line in cfg_lines: - ....: cfg_lines2.append( line + '\n' ) + ....: cfg_lines2.append(line + '\n') ....: In [15]: cfg_lines2 @@ -136,7 +135,7 @@ You can add line feed character in different ways. For example, you can simply p '!\n', 'ip ssh version 2\n', -If write the resulting list into a file, it already contains line feed characters: +If the final list is written anew to the file, then it will already contain newlines: .. code:: python