我正在开发一个程序,该程序使用线程和 file.seek 从互联网下载“大文件”(从 200mb 到 5Gb)以查找偏移量并将数据插入主文件,但是当我尝试将偏移量设置为 2147483647 字节以上时(超过 C long 最大值)它使int 太大而无法转换为 C long错误。我该如何解决这个问题?Bellow 是我的脚本代码的表示。
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)
Run Code Online (Sandbox Code Playgroud)
我不会问(因为已经问了很多)我是否真的找到了解决方案。
我读过关于将它转换为 int64 并使用类似 UL 的内容,但我并不真正理解它。我希望你能帮上忙,或者至少试着让我在脑海中更清楚这一点。xD
f.seek(3333333333, 4444444444)
Run Code Online (Sandbox Code Playgroud)
第二个参数应该是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
Run Code Online (Sandbox Code Playgroud)
并按3333333333
预期输出。