这对我来说不是一个新问题。从 C 到 PERL,再到 Windows Mobile、Windows XP 和其他 Windows 版本上的 Python,这个问题一直存在,让我心烦意乱。
现在,在我最新的脚本中,这种情况再次发生。
更具体地说:我用 Python 编写了一个简单的脚本。现在,当从调试器运行时,脚本可以正确写入文件,但在调试器之外,它无法正常工作。
它在应该写入文件时却没有写入。
我正在使用 python 2.6 和 eclipse 和 pydev。
这是代码
import httplib2
import thread
ht = httplib2.Http();
list = []
k = 0
def check(proxy, port):
global list
global k
try:
head = ht.request(proxy, 'HEAD')
except:
return
k = k + 1
list.append(proxy)
list.append(port)
def OnListCaller(ProxyList, OutFile, NListLen):
global list
global k
filei = open(ProxyList, 'r')
fileo = open(OutFile, 'a')
while 1:
proxy = filei.readline()
if not proxy: continue
port = filei.readline()
proxy = proxy.rstrip()
port = port.rstrip()
thread.start_new(check, (proxy, port,))
if k >= NListLen:
for t in list:
fileo.write(t + "\n")
list = []
fileo.close()
fileo = open(OutFile, 'a')
k = 0
OnListCaller('C:\proxy\input.txt', 'C:\proxy\checked.txt', 1)
问题出在 OnListCaller 处的 if k>=NListLen 处。
当 k >= 给定值时,应更新文件。
谢谢大家。
记住你妈妈教你的:
总是刷新()
(在Python中,file_object.flush()
其次是os.fsync(file_object.fileno())
)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)