0

experts, the problem is a common and I have tried based on the solutions and examples in SO but still have problem to write all of my devices print output into a file. I have test using several methods and most of the time I'm only got last line of output in my text file. The problem below almost same as mine...but no luck for me.

How to delete the contents of a file before writing into it in a python script?

I test the script to run specific command based on the type of device. If devices A run command A, if devices B run command B, if device C run command c. List of devices in text file ( the list have type and ip address). When i use 'w' the file content only the last line but when I use 'a' it will save all the content of all the devices in the list but when I run again the script it will continue to write on the last pointer and thus I got duplicate content..it append and keep append.

when use outFileName, "w" output content only take last line content

OUTPUT CONTENT for device type C IP 10.2.10.12

when use outFileName, "a" output content first time run the script as follows

OUTPUT CONTENT for device type A IP 192.168.100.100
OUTPUT CONTENT for device type A IP 192.168.100.110
OUTPUT CONTENT for device type B IP 10.1.10.100
OUTPUT CONTENT for device type C IP 10.2.10.10
OUTPUT CONTENT for device type C IP 10.2.10.11
OUTPUT CONTENT for device type C IP 10.2.10.12

when run script second time ...the file contains duplicates as follows

OUTPUT CONTENT for device type A IP 192.168.100.100
OUTPUT CONTENT for device type A IP 192.168.100.110
OUTPUT CONTENT for device type B IP 10.1.10.100
OUTPUT CONTENT for device type C IP 10.2.10.10
OUTPUT CONTENT for device type C IP 10.2.10.11
OUTPUT CONTENT for device type C IP 10.2.10.12
OUTPUT CONTENT for device type A IP 192.168.100.100
OUTPUT CONTENT for device type A IP 192.168.100.110
OUTPUT CONTENT for device type B IP 10.1.10.100
OUTPUT CONTENT for device type C IP 10.2.10.10
OUTPUT CONTENT for device type C IP 10.2.10.11
OUTPUT CONTENT for device type C IP 10.2.10.12

The script as follows

#Define functions for each device_type
def type_A(ip):
    return{
        'device_type': 'A',
        'ip': ip,
        'username': 'usr10',
        'password': 'password',
        }

def type_B(ip):
    return{
        'device_type': 'B',
        'ip': ip,
        'username': 'usr10',
        'password': 'password',
        }

def type_C(ip):
    return{
        'device_type': 'C',
        'ip': ip,
        'username': 'usr10',
        'password': 'password',
        }

#Function to create output text file
def writeOutFile(outFileName, string):
    with open(outFileName, "w") as f:
       outfile = f.write(string)

#Open Text file that contain device type and ip address
deviceFile = open('devices.txt','r')

#Create list llist for each line in the file.  
#The first item is the device type, 
#The second item is the IP address
for line in deviceFile:
    llist = line.split()
    ipAd = llist[1]

    #Check the first item of the list to determine device type and set 
    #variables
    if llist[0] == 'A':
        dvs = type_A(ipAd)
        sendCommand = 'command for device type A'
    elif llist[0] == 'B':
        dvs = type_B(ipAd)
        sendCommand = 'command for device type B'
    elif llist[0] == 'C':
        dvs = type_C(ipAd)
        sendCommand = 'command for device type c'
    else:
        print("no valid device type")
        break

    dvs_connect = ConnectHandler(**dvs)
    sendCommand = (dvs_connect.send_command(sendCommand))
    #This print all the devices output on the terminal
    print(sendCommand)

    #Generate output file
    outFileName = "outputFile.txt"

    #function call to write the output string into a text file
    writeOutFile(outFileName, sendCommand)

    dvs_connect.disconnect()

deviceFile.close()

devices.txt list

A 192.168.100.100
A 192.168.100.110
B 10.1.10.100
C 10.2.10.10
C 10.2.10.11
C 10.2.10.12

outputFile.txt The file output just content the last line content ...the output content of A and B seems already overwrite.

OUTPUT CONTENT for device type C IP 10.2.10.12

I expect how many time i run the script it will overwrite the existing content of text file but no duplicate...if I got 6 devices in my devices.txt list...meaning that I should have 6 device output in my text file output. The output file should be like below

OUTPUT CONTENT for device type A IP 192.168.100.100
OUTPUT CONTENT for device type A IP 192.168.100.110
OUTPUT CONTENT for device type B IP 10.1.10.100
OUTPUT CONTENT for device type C IP 10.2.10.10
OUTPUT CONTENT for device type C IP 10.2.10.11
OUTPUT CONTENT for device type C IP 10.2.10.12

I really hope someone could help me.. please assist. Thank you.

chenoi
  • 575
  • 3
  • 8
  • 30
  • the problem is not with opening the file in different modes, what you can do is save the file information in a pickle file, when you run it if u wanna store the ip of a device check if it is present in pickle dump file, if so skip else write to text file and at the end update the pickle file with the output – Surya Tej Oct 20 '19 at 03:08
  • Thanks sir...can you show how it done? – chenoi Oct 20 '19 at 10:01

1 Answers1

1

Opening with mode 'w' truncates the file if it exists, while mode 'a' will append to it.

Your issue is that you are re-opening the file with mode 'w' for each line of output. Every time you re-open the file, you clobber the previous contents.

There are two solutions to this problem:

  1. The preferred solution would be to open the file once in your script and use the existing file handle to write multiple lines to it, instead of opening a new file handle for each line of output.
  2. You could also open the file once with mode 'w' at the beginning of your script, then use mode 'a' from then on (but this is a hack).

One way to implement the first option would be to open the file right before the main loop:

with open("outputFile.txt", "w") as f:
    for line in deviceFile:
        # ...

        # Replace this line:
        # writeOutFile(outFileName, sendCommand)
        # With this one:
        f.write(sendCommand)

You may need to append a newline ("\n") to this string before writing it; I can't see how dvs_connect.send_command() formats its output.

cdhowie
  • 158,093
  • 24
  • 286
  • 300
  • Thanks for your response sir....option 1 is the one i try to do... i have tried open file in for loop but yet to open file before for loop...thanks for your response and example. I will try this. – chenoi Oct 20 '19 at 09:59