OSError:[Errno 22]参数无效:文件夹路径

时间:2017-12-07 09:49:20

标签: python

尝试将下载的图像保存到路径(文件夹内)。它适用于没有文件夹,我尝试过相对和绝对路径。

import requests
import os
import progressbar

b_url = "http://cdn2-img.pressreader.com/pressdisplay/docserver/getimage.aspx?file=9b342017090100000000001001&page={}&scale=100"

print("Format your url like this:")
print(b_url)
print("Note the {}\n")
print("Images will be written to the directory `PressReader`")
url = input("URL: ")
if url == "":
  url = b_url
name = input("Article name: ")
page_length = 84
bar = progressbar.ProgressBar()
for page in bar(range(0, page_length+1)):
  if not os.path.exists("PressReader"):
    os.makedirs("PressReader")
  r = requests.get(url.format(page), stream=True)
  if r.status_code == 200:
with open(r'"PressReader\{}-Page-{}.png"'.format(name, page), 'wb') as f:
  for chunk in r:
    f.write(chunk)

这应该创建一个文件夹PressReader(它可以),然后将图像下载到该文件夹​​。

然而,它出现了这个错误:

line 23, in <module>
with open(r'"PressReader\{}-Page-{}.png"'.format(name, page), 'wb') as f:
OSError: [Errno 22] Invalid argument: '"PressReader\\e-Page-1.png"'

0 个答案:

没有答案