Skip to content

Commit

Permalink
Chapter 07 update. Small fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
natenka committed Oct 20, 2020
1 parent cfecdcc commit 0d7dcb2
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 16 deletions.
2 changes: 1 addition & 1 deletion docs/source/book/07_files/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
2 changes: 2 additions & 0 deletions docs/source/book/07_files/open.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@ 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

* ``a`` - open file to add a data. Data is added to the end of file
* ``a+`` - open file for reading and writing. Data is added to the end of file

.. note::

r - read; a - append; w - write
9 changes: 5 additions & 4 deletions docs/source/book/07_files/read.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()``
^^^^^^^^^^^^^^
Expand Down
8 changes: 4 additions & 4 deletions docs/source/book/07_files/with.rst
Original file line number Diff line number Diff line change
@@ -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``:

Expand Down Expand Up @@ -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:

Expand Down Expand Up @@ -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
Expand Down
13 changes: 6 additions & 7 deletions docs/source/book/07_files/write.rst
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 0d7dcb2

Please sign in to comment.