首页 > 解决方案 > Python Wget:检查重复文件并跳过是否存在?

问题描述

所以我正在使用 WGET 下载文件,我想在下载之前检查文件是否存在。我知道 CLI 版本可以选择:(参见示例)

# check if file exsists
# if not, download
wget.download(url, path)

使用 WGET,它无需命名即可下载文件。这很重要,因为我不想在文件已有名称时重命名它们。

如果有允许检查现有文件的替代文件下载方法,请告诉我!谢谢!!!

标签: pythonwget

解决方案


wget.download()没有任何这样的选择。以下解决方法应该可以为您解决问题:

import subprocess

url = "https://url/to/index.html"
path = "/path/to/save/your/files"
subprocess.run(["wget", "-r", "-nc", "-P", path, url])

如果文件已经存在,您将收到以下消息:

File ‘index.html’ already there; not retrieving.

编辑: 如果你在 Windows 上运行它,你还必须包括shell=True

subprocess.run(["wget", "-r", "-nc", "-P", path, url], shell=True)

推荐阅读