我正在开发一个程序,使用线程和文件从 Internet 下载“大文件”(从 200mb 到 5Gb)。寻找偏移量并将数据插入主文件,但是当我尝试将偏移量设置为以上时2147483647 字节(超过 C long 最大值)它给出了 int too large to convert to C long 错误。我该如何解决这个问题?下面是我的脚本代码的表示。
f = open("bigfile.txt")
#create big file
f.seek(5000000000-1)
f.write("\0")
#try to get the offset, this gives the error (Python int too large to convert to C long)
f.seek(3333333333, 4444444444)
如果我真的找到了解决方案,我不会问(因为它被问了很多)。
我读到有关将其转换为 int64 并使用类似 UL 的内容,但我并不真正理解它。我希望你能帮忙,或者至少试着让我的头脑更清楚这一点。 xD
最佳答案
f.seek(3333333333, 4444444444)
第二个参数应该是 from_where
参数,指示您是否从以下位置寻找:
- 文件开始,
os.SEEK_SET
或0
; - 当前位置,
os.SEEK_CUR
或1
; - 文件结尾,
os.SEEK_END
或2
。
4444444444
不是允许的值之一。
下面的程序运行良好:
import os
f = open("bigfile.txt",'w')
f.seek(5000000000-1)
f.write("\0")
f.seek(3333333333, os.SEEK_SET)
print f.tell() # 'print(f.tell())' for Python3
并按预期输出 3333333333
。
关于Python : file. 寻找(10000000000、2000000000)。 Python int 太大而无法转换为 C long,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33206710/