FileNotFoundError:[Errno 2]没有这样的文件或目录不能解决路径问题

2024-09-30 02:36:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我有这个问题,我试图运行脚本来下载Springers免费书籍[https://towardsdatascience.com/springer-has-released-65-machine-learning-and-data-books-for-free-961f8181f189],但是很多事情开始出错。 我解决了一些问题,但现在我陷入困境

    C:\Windows\system32>python C:\Users\loren\Desktop\springer_free_books-master\main.py
Traceback (most recent call last):
  File "C:\Users\loren\Desktop\springer_free_books-master\main.py", line 42, in <module>
    books.to_excel(table_path)
  File "C:\Users\loren\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pandas\core\generic.py", line 2175, in to_excel
    formatter.write(
  File "C:\Users\loren\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pandas\io\formats\excel.py", line 738, in write
    writer.save()
  File "C:\Users\loren\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pandas\io\excel\_openpyxl.py", line 43, in save
    return self.book.save(self.path)
  File "C:\Users\loren\AppData\Local\Programs\Python\Python38-32\lib\site-packages\openpyxl\workbook\workbook.py", line 392, in save
    save_workbook(self, filename)
  File "C:\Users\loren\AppData\Local\Programs\Python\Python38-32\lib\site-packages\openpyxl\writer\excel.py", line 291, in save_workbook
    archive = ZipFile(filename, 'w', ZIP_DEFLATED, allowZip64=True)
  File "C:\Users\loren\AppData\Local\Programs\Python\Python38-32\lib\zipfile.py", line 1251, in __init__
    self.fp = io.open(file, filemode)
FileNotFoundError: [Errno 2] No such file or directory: 'downloads\\table_v4.xlsx'

这是代码的一部分,介绍了WER表的路径

table_url = 'https://resource-cms.springernature.com/springer-cms/rest/v1/content/17858272/data/v4'

table = 'table_' + table_url.split('/')[-1] + '.xlsx'
table_path = os.path.join(folder, table)
if not os.path.exists(table_path):
    books = pd.read_excel(table_url)
    # Save table
    books.to_excel(table_path)
else:
    books = pd.read_excel(table_path, index_col=0, header=0)

Tags: pathinpyliblocallinetablebooks
1条回答
网友
1楼 · 发布于 2024-09-30 02:36:07

在调用.to_excel()之前,请尝试创建目标目录,以确保存在有效的可写目录。确保导入了os模块:

import os      # add to your imports

替换

    books.to_excel(table_path)

    os.makedirs(folder, exist_ok=True)
    books.to_excel(table_path)

相关问题 更多 >

    热门问题