1

I have this code:

ipaddr = input("Enter IP Address: ")
devicename = input ("Enter Device Name: ")

for i, d in zip(ipaddr.split(), devicename.split()):
   print("Your IP is: ", i, "Your device is: ", d)

My output is this:

Enter IP Address: 1 2 3 4 5

Enter Device Name: d1 d2 d3 d4 d5

Your IP is:  1 Your device is:  d1
Your IP is:  2 Your device is:  d2
Your IP is:  3 Your device is:  d3
Your IP is:  4 Your device is:  d4
Your IP is:  5 Your device is:  d5

I would like each line above (IP and device name combination) to be saved into individual txt files like this:

C:\Scripts
d1.txt
d2.txt
d3.txt
d4.txt
d5.txt

I am looking into creating another program to read those files individually some other time. For now I am looking into saving my output into different files. Hoping you could help. Thank you!

2 Answers 2

1

This will work, You can add a variable in string with the following syntax:

variable=33
a="%s"%variable
print(a)

output will be:

'33'

ipaddr = input("Enter IP Address: ")
devicename = input ("Enter Device Name: ")

for i, d in zip(ipaddr.split(), devicename.split()):
   print("Your IP is: ", i, "Your device is: ", d)
   with open("%s.txt"%d,"w")as file: #opens a file with value of d.txt as file name to write
        file.write("Your IP is: %s Your device is %s"%(i,d))
Sign up to request clarification or add additional context in comments.

1 Comment

This worked like a charm sir. Exactly what I needed. Thank you so much!
0

All you realistically have to add is file creation to your loop, as follows:

for i, d in zip(ipaddr.split(), devicename.split()):
     with open("d%d.txt"%i, "w") as fp:
          fp.write("Your IP is: ", i, "Your device is: ", d)

Comments

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.