1
我使用以下代碼逐行讀取帶有主機地址的txt文件,但是當代碼創建並寫入文件作爲日誌時,它會用引號和'?'寫入文件。性格:寫入文件時不使用引號
的代碼是這樣的:
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()
所以創建的文件是這樣的:
[[email protected] Projeto]$ ls
21.10.176.4?.txt clean_up_819 hosts.txt master.py test1.txt teste.txt
後來,當我使用貓文件與問號displaing:
[[email protected] Projeto_QoS]$ cat '21.10.176.4
.txt'
有什麼辦法可以保存在一個標準化的文件中只有21.10.176.4.txt?