You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用gdown下载Google Drive文件时遇扩展名不支持错误求助

问题排查与解决方案

错误根源

你遇到的ValueError: Unsupported file extension是因为**download_and_extract函数在判断文件类型时,用了原始的Google Drive网页链接(而非下载后的本地文件路径)**。Google Drive的网页链接本身没有.tgz这类文件扩展名,所以触发了不支持的格式判断。

另外,你的file_id提取逻辑存在缺陷:你提供的链接是https://drive.google.com/file/d/10GRqLu6-1JPXI8rcq23S4-4AhB6On-L6/view,这种格式的id在/d/和/view之间,用split('=')[-1]无法正确提取到file_id,会导致后续下载链接无效。

修复步骤

1. 修复Google Drive文件ID提取逻辑

替换原来的file_id提取代码,适配两种常见的Drive链接格式:

def get_gdrive_file_id(url):
    if "/file/d/" in url:
        # 处理 https://drive.google.com/file/d/xxx/view 格式
        return url.split("/file/d/")[1].split("/")[0]
    elif "id=" in url:
        # 处理 https://drive.google.com/open?id=xxx 格式
        return url.split("id=")[1].split("&")[0]
    else:
        raise ValueError("无法识别的Google Drive链接格式")

file_id = get_gdrive_file_id(url)
gdrive_url = f"https://drive.google.com/uc?id={file_id}"
output_path = os.path.join(extract_to, "file.tgz")
gdown.download(gdrive_url, output_path, quiet=False)

2. 修改download_and_extract的扩展名判断逻辑

找到函数里判断文件扩展名的代码,把基于原始url判断改成基于本地输出文件路径判断:

# 替换原来的 ext = os.path.splitext(url)[1]
ext = os.path.splitext(output_path)[1].lower()

if ext in (".tar", ".tgz", ".tar.gz"):
    tar_ref = tarfile.open(output_path, "r:*")
    tar_ref.extractall(path=extract_to)
elif ext == ".zip":
    with zipfile.ZipFile(output_path, "r") as zip_ref:
        zip_ref.extractall(extract_to)
else:
    raise ValueError(f"不支持的文件扩展名: {ext}")

3. 简化下载方式(可选)

直接使用gdown的id参数下载,无需手动拼接uc链接:

gdown.download(id=file_id, output=output_path, quiet=False)

验证

修改后,download_and_extract会基于你指定的file.tgz扩展名进行判断,而非原始的Drive网页链接,即可正常完成下载与解压操作。

内容的提问来源于stack exchange,提问作者pceccon

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 17:17:34