有些数据需要放到LC上, 顺手在win上用python的sdk写了个脚本,不想踩了个坑
当然, 严格意义上说, 不算是LC的锅. 这个问题还是蛮有普遍意义的,发出来分享下
python文档里上传文件的例子如下:
f = open(avatar_file_path)
avatar_image = File('avatar.png', f)
f.close()
avatar_image.save()
这段代码运行在windows(win7)上绝大多数情况会有错误的, linux上不会有问题.
核心问题在File类会调用python file的read方法读取文件内容,问题就出在这里:
| read(...)
| read([size]) -> read at most size bytes, returned as a string.
| If the size argument is negative or omitted, read until EOF is reached. Notice that when in non-blocking mode, less data than what was requested may be returned, even if no size parameter was given.
windows下的python2 read方法是非阻塞的,所以read只读取了部分就返回了.
找到原因了就好解决了,不用文件接口,自己实现个循环读的函数就好了,
def my_read_file(file):
data = ''
while True:
s = file.read()
if len(s) == 0:
return data
data = data + s
注意文件打开时要加 'b'选项,否则还是读不全
python3的read方法试了在什么os下都不会有这个问题,可以一次性读全.
LC什么时候能提供python3的版本啊
-
创建时间
16年4月8日
-
最后回复
16年6月7日
-
2
回复
-
2.1K
浏览
-
3
用户
-
2
赞
-
1
链接