简体   繁体   English

Python write()不写入完整文件

[英]Python write() doesn't write full file

For some reason that I cannot figure out this will only write 11571 characters in the pi.txt file - way short of the 50 thousand there should be I've been looking up buffers to do with the disk IO, and have tried os.fsync(fo) every 10 thousand iterations, but the same thing happens, only 11571 characters actually recorded in the txt file It seems to be generating all 50 thousand chars (it counts up to 50k with the Count status: ) Is this a problem with my code, or disk buffer or something simple I am completely missing. 由于某种原因,我无法弄清楚这只会在pi.txt文件中写入11571个字符-距离5万个字符还差得很远,我应该一直在查找与磁盘IO有关的缓冲区,并尝试了os.fsync (fo)每1万次迭代,但是同样的事情发生了,实际上txt文件中仅记录了11571个字符似乎正在生成所有5万个字符(以Count状态计数到50k):这是我的问题吗代码,磁盘缓冲区或一些简单的东西,我完全不知道。 this code was nicked from here: 1000 digits of pi in python 这段代码从这里被昵称: python中pi的1000位数字

I've tried this solution: Python not writing full string to file 我尝试了以下解决方案: Python未将完整字符串写入文件

Exactly the same thing, write down to the size of the file 完全一样,写下文件的大小

I've also tried calculating longer and shorter values, same issue. 我也尝试过计算较长和较短的值,同样的问题。

import os
fo=open("pi.txt", "wb")
def make_pi():
    cnt = 1
    q, r, t, k, m, x = 1, 0, 1, 1, 3, 3
    for j in range(50000):
        if 4 * q + r - t < m * t:
            fo.write(str(m))
            yield m
            q, r, t, k, m, x = 10*q, 10*(r-m*t), t, k, (10*(3*q+r))//t - 10*m, x
        else:
            q, r, t, k, m, x = q*k, (2*q+r)*x, t*x, k+1, (q*(7*k+2)+r*x)//(t*x), x+2
        if cnt % 1000 == 0:
            print("Count status: %s" % cnt)
        elif cnt % 10002 == 0:
            fo.flush()
            os.fsync(fo)
        else:
            pass
        cnt+=1
for i in make_pi():
    pass
fo.close()

Also if it makes a difference I am using windows 7 64 bit, I haven't tried this in linux. 另外如果我使用的是Windows 7 64位操作系统,那么我没有在linux中尝试过。

This is the fixed code, that is working correctly :) 这是固定的代码,可以正常工作:)

fo=open("pi.txt", "wb")
def make_pi():
    cnt=0
    q, r, t, k, m, x = 1, 0, 1, 1, 3, 3
    while True:
        if 4 * q + r - t < m * t:
            if cnt % 1000 == 0:
                print("Counted %s places" % cnt)
            else:
                pass
            yield m
            fo.write(str(m))
            cnt+=1
            q, r, t, k, m, x = 10*q, 10*(r-m*t), t, k, (10*(3*q+r))//t - 10*m, x
        else:
            q, r, t, k, m, x = q*k, (2*q+r)*x, t*x, k+1, (q*(7*k+2)+r*x)//(t*x), x+2

i = 0;
for x in make_pi():
    i += 1
    if i == 10001:
        break

fo.close()

Thanks for pointing that out :) 感谢您指出了这一点 :)

This is a logical issue and not a buffering/disc syncing one. 这是一个逻辑问题,而不是缓冲/光盘同步的问题。 The function only yields (and writes) if the condition: 4 * q + r - t < m * t is TRUE, which it is only 11571 times in the 50K loops. 该函数仅在以下条件下产生(并写入): 4 * q + r - t < m * t为TRUE,在50K循环中仅为11571次。

Just because.. here's a slightly modified version which should achieve what you're trying to do: generate the first X digits of PI: 只是因为..这是一个经过稍微修改的版本,应该可以实现您要执行的操作:生成PI的前X个数字:

fo=open("pi.txt", "wb")

def make_pi(num_digits):
    q, r, t, k, m, x = 1, 0, 1, 1, 3, 3
    j = 0
    while True:
        if 4 * q + r - t < m * t:
            fo.write(str(m))
            yield m
            q, r, t, k, m, x = 10*q, 10*(r-m*t), t, k, (10*(3*q+r))//t - 10*m, x
            j += 1
            if j % 1000 == 0:
                print("Count status: %s" % j)
        else:
            q, r, t, k, m, x = q*k, (2*q+r)*x, t*x, k+1, (q*(7*k+2)+r*x)//(t*x), x+2
        if j >= num_digits:
            break

for i in make_pi(50000):
    pass

fo.close()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM