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!

6
  • f = open(..., 'w'), f.write(... + "\n"), f.close() Commented Dec 13, 2019 at 4: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 .stackoverflow.com/questions/5214578/print-string-to-text-file Commented Dec 13, 2019 at 4: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 Commented Dec 13, 2019 at 4:39
  • You can also simply redirect the output of running this script to a text file, for example python myscript.py > output.txt. Commented Dec 13, 2019 at 4: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? Commented Dec 13, 2019 at 4:45

2 Answers 2

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)
Sign up to request clarification or add additional context in comments.

1 Comment

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?
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()

2 Comments

This is only going to write output based on the last line read from the input file.
Yeah figured. Edited.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.