所以我正在使用 WGET 下载文件,我想在下载之前检查文件是否存在。我知道 CLI 版本有一个选项:(see example) .
# check if file exsists
# if not, download
wget.download(url, path)
使用 WGET,它无需命名即可下载文件。这很重要,因为我不想在文件已有名称时重命名它们。
如果有其他文件下载方法可以检查现有文件,请告诉我!谢谢!!!
最佳答案
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)
关于Python Wget : Check for duplicate files and skip if it exists?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55524747/