1

I using the following code to read a txt file with host addresses, line by line, but when the code create and write to file as log, it writing with quoting marks and '?' character:

The code is this:

import getpass
import sys
import telnetlib

user = "cisco"
password = "cisco"

file = open('hosts.txt', 'r')
for line in file:

        tn = telnetlib.Telnet(line)

        tn.read_until("Username: ")
        tn.write(user + "\n")
        tn.read_until("Password: ")
        tn.write(password + "\n")
        tn.write("enable \n")
        tn.write(password + "\n")

        tn.write("sh ver | i revision \n")
        tn.write("exit \n")

        str_all = tn.read_all()
        log = open(line + ".txt","w")
        log.write(str_all)
        tn.close()

So the file created is this:

[temp@ser1 Projeto]$ ls
21.10.176.4?.txt  clean_up_819  hosts.txt  master.py  test1.txt  teste.txt

Then when I use cat the file is displaing with question marks:

[noctemp@svcactides Projeto_QoS]$ cat '21.10.176.4
.txt'

Is there any way to save in a file normalized called only 21.10.176.4.txt ?

Thiago
  • 35
  • 1
  • 4

1 Answers1

0

Your file name does not have quotes; those were added by your shell's tab-completion to protect the newline that it does have.

The newline came from line, which has it so that you can detect whether the last line has one (and for convenience when re-writing a similar file). See How can I remove (chomp) a newline in Python?.

Davis Herring
  • 36,443
  • 4
  • 48
  • 76