2

I am very new to scripting (this is my first one) and I'm trying to automate network tasks with python. I have built a script that takes a list of AP names from a text file and puts those ap names into the appropriate place within lines of configuration.

What I would really like is to have the final result saved to a file instead of printed to screen, and nothing I've tried yet has worked. Here's my script that prints to screen

f1=open("filename.txt","r")
Lines=f1.readlines()
for line in Lines:
line = line.strip()
    o1="ap name " + (line) + " lan port-id 1 enable"
    o2="ap name " + (line) + " lan port-id 2 enable"
    o3="ap name " + (line) + " lan port-id 3 enable"
    print(o1)
    print(o2)
    print(o3)

f1.close()

So, this works but then I'm having to copy paste it out of the print. I'd love to have it automatically export to a text file, but none of the things I've tried yet have worked. Thanks for the help!

Smooshface
  • 23
  • 3
  • `f = open(..., 'w')`, `f.write(... + "\n")`, `f.close()` – furas Dec 13 '19 at 04:36
  • Did you try finding the answer ? I am sure you will find the answer on Stack Overflow itself! For example Take a look into this answer .https://stackoverflow.com/questions/5214578/print-string-to-text-file – McLovin Dec 13 '19 at 04:37
  • So I did f2=open("output.txt","w") as line 2 and instead of the print commands used f2.write(o1) and the document stayed blank – Smooshface Dec 13 '19 at 04:39
  • You can also simply redirect the output of running this script to a text file, for example `python myscript.py > output.txt`. – jarmod Dec 13 '19 at 04:39
  • Do you really want to use the same line of input for all three ports, or do you have 3 lines of input, each of which should be assigned to a different port? – chepner Dec 13 '19 at 04:45
  • Chepner your answer you gave works perfectly. In THIS case, it's 3 different commands, per device, so 3 separate lines. – Smooshface Dec 13 '19 at 04:48

2 Answers2

1

You just need to open a file for writing and tell print to use that file via the file argument, instead of sys.stdout.

with open("filename.txt", "r") as input, open("newfile", "w") as output:
    for line in input:
        line = line.strip()
        for i in [1,2,3]:
            print(f"ap name {line} lan port-id {i} enable", file=output)
chepner
  • 497,756
  • 71
  • 530
  • 681
  • Thank you VERY much for the assistance, it works perfectly (as I'm sure you know). I have a follow-up question. So, I am very new to Python (installed it and looked at it for the first time last night and got to where I was) I understand everything I see in your solution except the f after print. What does the f do? – Smooshface Dec 13 '19 at 14:45
0

The problem maybe with you opening in "w" mode as it erases and rewrites. Try "a" mode.

f1=open("filename.txt","r")
Lines=f1.readlines()
for line in Lines:
line = line.strip()
    o1="ap name " + (line) + " lan port-id 1 enable"
    o2="ap name " + (line) + " lan port-id 2 enable"
    o3="ap name " + (line) + " lan port-id 3 enable"
    print(o1)
    print(o2)
    print(o3)
    f2 = open("result.txt","a")
    f2.write("o1: %s\n" % o1)
    f2.write("o2: %s\n" % o2)
    f2.write("o3: %s\n" % o3)
    f2.close()

f1.close()
Sharan
  • 691
  • 1
  • 7
  • 16