使用什么命令代替 urllib.request.urlretrieve? [英] What command to use instead of urllib.request.urlretrieve?

查看:36
本文介绍了使用什么命令代替 urllib.request.urlretrieve?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在编写一个从 URL 下载文件的脚本

导入 urllib.requesturllib.request.urlretrieve(my_url, 'my_filename')

根据文档,urllib.request.urlretrieve 是一个遗留接口,可能会被弃用,因此我想避免使用它,这样我就不必在不久的将来重写此代码.

我在标准库中找不到像 download(url, filename) 这样的另一个接口.如果 urlretrieve 被认为是 Python 3 中的遗留接口,那么替代品是什么?

解决方案

已弃用 是一回事,将来可能会被弃用 是另一回事.>

如果它适合您的需求,我会继续使用 urlretrieve.

也就是说,你可以不用它:

from urllib.request import urlopen从shutil 导入copyfileobj使用 urlopen(my_url) 作为 in_stream,open('my_filename', 'wb') 作为 out_file:copyfileobj(in_stream, out_file)

I'm currently writing a script that downloads a file from a URL

import urllib.request
urllib.request.urlretrieve(my_url, 'my_filename')

According to the docs, urllib.request.urlretrieve is a legacy interface and might become deprecated, therefore I would like to avoid it so I don't have to rewrite this code in the near future.

I'm unable to find another interface like download(url, filename) in standard libraries. If urlretrieve is considered a legacy interface in Python 3, what is the replacement?

解决方案

Deprecated is one thing, might become deprecated at some point in the future is another.

If it suits your needs, I'd continuing using urlretrieve.

That said, you can do without it:

from urllib.request import urlopen
from shutil import copyfileobj

with urlopen(my_url) as in_stream, open('my_filename', 'wb') as out_file:
    copyfileobj(in_stream, out_file)

这篇关于使用什么命令代替 urllib.request.urlretrieve?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆