Python Wget:检查重复文件,然后跳过是否存在?

641*_*130 5 python wget

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

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

Run Code Online (Sandbox Code Playgroud)

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

如果还有其他文件下载方法可以检查现有文件,请告诉我!谢谢!!!

Gio*_*ous 3

wget.download()没有任何这样的选项。以下解决方法应该可以帮助您:

\n\n
import subprocess\n\nurl = "https://url/to/index.html"\npath = "/path/to/save/your/files"\nsubprocess.run(["wget", "-r", "-nc", "-P", path, url])\n
Run Code Online (Sandbox Code Playgroud)\n\n

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

\n\n
File \xe2\x80\x98index.html\xe2\x80\x99 already there; not retrieving.\n
Run Code Online (Sandbox Code Playgroud)\n\n

编辑: \n如果您在 Windows 上运行此程序,您还必须包括shell=True

\n\n
subprocess.run(["wget", "-r", "-nc", "-P", path, url], shell=True)\n
Run Code Online (Sandbox Code Playgroud)\n