python - 程序流中应该发生的文件写入没有发生

标签 python

这对我来说不是一个新问题。在 Windows Mobile、Windows XP 和其他 Windows 版本上,从 C 到 PERL 再到 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)   

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

最佳答案

记住你妈妈教你的:

总是刷新()

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

关于python - 程序流中应该发生的文件写入没有发生,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1991815/

相关文章:

Python Flask - 如何将值从一个路由传递到另一个路由?

Python 2.7 类属性奇怪的行为

python - 在嵌套列表的开头插入一个列表

python - Pandas 返回不一致的列值计数

Python 脚本错误

python - 如何从 gensim 0.11.1 中的 Doc2Vec 获取文档向量?

python - 将这两个列表过滤为一个的更好方法是什么?

python - AWS lambda 函数无法访问互联网

python - 删除python中包含数字的行

python - 如何收集网页中的所有链接?