Saving outputs in a single file without overwriting the previous output

Below is a piece of a long code. The last step should save the output of all devices in a single file. It should add up, not overwrite.
Now I’m getting only the output of the last device.

from netmiko import Netmiko
with open('addresses.txt') as f1:
with open('commands.txt') as f2:

for ip in file1:
    device = {'device_type':'cisco_ios',
    ssh = Netmiko(**device)
    file3 = open( '_show.txt', 'w')
    for cmd in file2:
        output = ssh.send_command(cmd)
        file3.write(output + '\n')

Try changing the mode to a, which will append the data: mode ‘w’ will create a new file…

file3 = open( '_show.txt', mode='a')
1 Like

waw… awesome… it works properly now…
I though mode w is only for writing permissions. But it seems doing more than that (creating a new file every time)
So, mode a gives write permissions to the same file. Great!

No worries.

There are six access modes (that I know of):

  1. 'r' Read only: the default
  2. 'w' Write: Create a new file or overwrite the contents of an existing file.
  3. 'a' Append: Write data to the end of an existing file.
  4. 'r+' Both read and write to an existing file. The file pointer will be at the beginning of the file.
  5. 'w+' Both read and write to a new file or overwrite the contents of an existing file.
  6. 'a+' Both read and write to an existing file or create a new file. The file pointer will be at the end of the file.

Use python’s built-in documentation tools, see that there are text and binary modes, too.

$ python -m pydoc open
    Character Meaning
    --------- ---------------------------------------------------------------
    'r'       open for reading (default)
    'w'       open for writing, truncating the file first
    'x'       create a new file and open it for writing
    'a'       open for writing, appending to the end of the file if it exists
    'b'       binary mode
    't'       text mode (default)
    '+'       open a disk file for updating (reading and writing)
    'U'       universal newline mode (deprecated)
    ========= ===============================================================

    The default mode is 'rt' (open for reading text). For binary random
    access, the mode 'w+b' opens and truncates the file to 0 bytes, while
    'r+b' opens the file without truncation. The 'x' mode implies 'w' and
    raises an `FileExistsError` if the file already exists.

Corresponding Stack Overflow canonical:

1 Like

A good idea


is there a way to get this info from the IDLE interpreter (shell)?

By the way, when in pydocs, and I type: file operations:

There is a plethora of different options of which none appear relating to this subject matter. How do you zoom in on this topic in pydocs?

Try this on the idle command line (or in any variant of the repl).

1 Like

Thank you very much :smiley:

Interesting that ‘help’ zooms in to the file operations and not some related keyword.
open’ seems very general.

It’s the name of the function, the help function is just formatting and displaying its doc-string, try this:

1 Like

Note that while the search ordering is not optimal (and we’re working on improving it), searching for open will show the builtin open() in the list (albeit not as the first item, as ideally it should), and leads to a HTML reference documentation page that includes a detailed list and description of the mode fags.

Thank you. Much appreciated.