如果应该在程序流程中发生文件写入,则不会发生

190*_*mer 2 python

这对我来说不是一个新问题.从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)   
Run Code Online (Sandbox Code Playgroud)

问题出现在if> k> = NListLen的OnListCaller中.当k>> =然后给定值时,应更新该文件.谢谢大家.

Jam*_*mes 8

记住你妈妈教给你的东西:

总是冲洗()

(在python中,file_object.flush()后跟os.fsync(file_object.fileno()))