OSError:[Errno 22]无效的参数写入tar文件

问题描述 投票:0回答:1

我正在尝试下载2.5 GB的tar文件,然后使用Python将其写入磁盘。使用其他.tar文件,以下命令可以正常运行,但是使用以下指定的tar文件崩溃:

import requests

url = 'http://pixplot.yale.edu/datasets/bain/photos.tar'
with open('photos.tar', 'wb') as out:
  r = requests.get(url, allow_redirects=True)
  out.write(r.content)

这引起了:

<class 'requests.models.Response'>
Traceback (most recent call last):
  File "t.py", line 7, in <module>
    out.write(r.content)
OSError: [Errno 22] Invalid argument

有人知道什么可能导致此错误吗?任何建议都会有所帮助!

NB:仅在Python 3.x中会出现此问题。如果我切换到2.7 conda环境,则上面的代码段运行良好。

python python-requests operating-system
1个回答
1
投票

听起来好像在向文件中写入大量文件时遇到问题,请尝试将其拆分为较小的块:

blocksize = 1000000000
for i in range(0, len(r.content), blocksize):
    out.write(r.content[i:i+blocksize])

我猜Python 2.7是在内部完成的,而3.5则没有。

© www.soinside.com 2019 - 2024. All rights reserved.