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

2024-10-01 00:27:43 发布

您现在位置: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(它确实如此),然后将图像下载到该文件夹中。在

但是它会断开,并出现以下错误:

^{pr2}$

Tags: thename图像import文件夹urlinputif