gpt4 book ai didi

python - Python 中 'wb' 文件模式下的 FileNotFoundError?

转载 作者:太空宇宙 更新时间:2023-11-04 04:30:54 26 4
gpt4 key购买 nike

我正在尝试编写一个程序来下载所有 xkcd 漫画图像并将它们保存在一个目录中,所有图像名称为 title.png,title 是漫画的标题。这是它的代码:

#Downloads all the xkcd comics

import requests, bs4, os

site = requests.get('https://www.xkcd.com')

def downloadImage(site):
soup = bs4.BeautifulSoup(site.text)
img_tag = soup.select('div[id="comic"] img')
img_title = img_tag[0].get('alt')
img_file = open(img_title+'.png', 'wb')
print("Downloading %s..." %img_title)
img_res = requests.get("https:" + img_tag[0].get('src'))
for chunk in img_res.iter_content(100000):
img_file.write(chunk)
print("Saved %s in " %img_title, os.getcwd())


def downloadPrevious(site):
soup = bs4.BeautifulSoup(site.text)
prev_tag_list = soup.select("ul[class='comicNav'] li > a")
prev_tag = None
for each in prev_tag_list:
if(each.get('rel')==['prev']):
prev_tag = each
break
if(prev_tag.get('href') == '#'):
return True
prev_site = requests.get('https://xkcd.com' + prev_tag.get('href'))
downloadImage(prev_site)
return False, prev_site

def download_XKCD_Comics(site):
try:
os.makedirs('E:\\XKCD Comics')
except:
os.chdir('E:\XKCD Comics')

done = False
downloadImage(site)
while(not done):
done, site = downloadPrevious(site)
return

download_XKCD_Comics(site)

代码的输出:

==== RESTART: E:\Computer_Science_Programs\Python\Get all XKCD Comics.py ====
Downloading Data Pipeline...
Saved Data Pipeline in E:\XKCD Comics
Downloading Incoming Calls...
Saved Incoming Calls in E:\XKCD Comics
Downloading Stanislav Petrov Day...
Saved Stanislav Petrov Day in E:\XKCD Comics
Downloading Bad Opinions...
Saved Bad Opinions in E:\XKCD Comics
Traceback (most recent call last):
File "E:\Computer_Science_Programs\Python\Get all XKCD Comics.py", line 45, in <module>
download_XKCD_Comics(site)
File "E:\Computer_Science_Programs\Python\Get all XKCD Comics.py", line 42, in download_XKCD_Comics
done, site = downloadPrevious(site)
File "E:\Computer_Science_Programs\Python\Get all XKCD Comics.py", line 30, in downloadPrevious
downloadImage(prev_site)
File "E:\Computer_Science_Programs\Python\Get all XKCD Comics.py", line 11, in downloadImage
img_file = open(img_title+'.png', 'wb')
FileNotFoundError: [Errno 2] No such file or directory: '6/6 Time.png'
>>>

我不明白这个问题。其他文件都不存在,但仅使用此文件名引发错误。请有人帮我解决这个问题!

最佳答案

我刚刚遇到一个问题,在 wb 模式下打开文件时出现 FileNotFoundError: [Errno 2] No such file or directory:,这让我很困惑,因为我认为使用 openwb 应该创建文件(如果它不存在)。原来问题是我试图创建的文件位于一个不存在的目录中。轻松修复:

MNIST_DATA_DIRNAME = os.path.dirname(MNIST_DATA_FILENAME)
if not os.path.isdir(MNIST_DATA_DIRNAME):
os.makedirs(MNIST_DATA_DIRNAME)

with open(MNIST_DATA_FILENAME, "wb") as f:
f.write(b)

关于python - Python 中 'wb' 文件模式下的 FileNotFoundError?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52657610/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com