python - Python Wget:检查重复文件并跳过是否存在?
问题描述
所以我正在使用 WGET 下载文件,我想在下载之前检查文件是否存在。我知道 CLI 版本可以选择:(参见示例)。
# 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)
推荐阅读
- android - 是否可以将彩色框架/边框作为 Android 服务添加到屏幕上,以便用户可以在框架存在时浏览应用程序?
- visual-c++ - 如何在我的 C++ 程序中删除退格键
- azure-service-fabric - 阻止对 Azure Service Fabric WebAPI 服务的“垃圾”请求
- xamarin - Xamarin iOS - 以任何语言加载资源而无需使用手机的语言
- java - Arraylist 中使用的重复数据
- angular - 如何使用指令长按 Ionic2
- javascript - ERROR 错误:StaticInjectorError(AppModule)[MatToolbar -> ElementRef]
- c# - 根据给定日期从列表中查找项目
- php - 如何将此 json 响应的值设置为 php 变量?
- javascript - Firebase Cloud Functions JavaScript - 如何在一个函数中从不同表中获取多个数据