[英]Text file editing and line writing
我的代碼有一個小問題。
首先是代碼:
import os
import subprocess
import httplib, urllib
for ip in range(1,255):
ip_addr = "192.168.1." + str(ip)
res = subprocess.call(["ping", ip_addr, "-c1", "-W1", "-q"], stdout=open(os.devnull,'w'))
if res == 0:
print (ip_addr) + " reachable"
else:
print (ip_addr) + " not reachable"
f = open('state_All.txt','r')
prestate = f.readline(ip)
f.close()
prestate = ord(prestate)
res = res + 97
if not prestate == res:
if prestate == 97:
conn = httplib.HTTPSConnection("api.pushover.net:443")
## The Code in here which I have deleted out is definitely not the problem,
## works 100% safe and can't be share beacuse of security problems.
}), { "Content-type": "application/x-www-form-urlencoded" })
conn.getresponse()
else:
conn = httplib.HTTPSConnection("api.pushover.net:443")
## The Code in here which I have deleted out is definitely not the problem,
## works 100% safe and can't be share beacuse of security problems.
}), { "Content-type": "application/x-www-form-urlencoded" })
conn.getresponse()
f = open('state_All.txt','w')
res = str(unichr(res))
data = (res[ip])
f.writelines(data)
f.close()
問題:
讀取.txt文件中的某些特定行有效,但此后我無法在特定行中寫入測量狀態,
我收到此錯誤:
Traceback (most recent call last):
File "ping_All.py", line 48, in <module>
data = (res[ip])
IndexError: string index out of range
希望有人可以幫助我。
當您嘗試從該字符串訪問項目時, index out of range
。 這意味着您嘗試訪問索引( ip
)大於res
長度或小於0的項目(以您的情況為Character)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.