这对我来说不是一个新问题。在 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/