python urlretrieve通过HTTP下载文件的默认路径 [英] The default path of python urlretrieve downloading file via HTTP

查看:90
本文介绍了python urlretrieve通过HTTP下载文件的默认路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们知道我们可以使用urllib.urlretrieve通过HTTP将文件下载到本地文件系统.例如:

We know that we could use urllib.urlretrieve to download file via HTTP to local file system. For example:

import urllib
urllib.urlretrieve ("http://www.example.com/songs/mp3.mp3", "mp3.mp3")

我想知道如果我们下载mp3.mp3之类的文件的默认路径在哪里?我已经阅读了python文档.

I wonder where is the default path if we download a file like mp3.mp3? I have read python document.

urllib.urlretrieve(url [,filename [,reporthook [,data]]]) 如有必要,将URL表示的网络对象复制到本地文件.如果URL指向本地文件,或者存在对象的有效缓存副本,则不会复制该对象.返回一个元组(文件名,标头),其中filename是可以在其中找到对象的本地文件名,标头是由urlopen()返回的对象的info()方法返回的(对于远程对象,可能是缓存的) .异常与urlopen()相同. 第二个参数(如果存在)指定要复制到的文件位置(如果不存在,该位置将是具有生成名称的临时文件).第三个参数(如果存在)是一个挂钩函数,它将在建立网络连接时被调用一次,并在随后的每个块读取之后被调用一次.钩子将被传递三个参数.到目前为止已传输的块数,以字节为单位的块大小以及文件的总大小.在较旧的FTP服务器上,第三个参数可能为-1,这些FTP服务器不会响应检索请求而返回文件大小.

urllib.urlretrieve(url[, filename[, reporthook[, data]]]) Copy a network object denoted by a URL to a local file, if necessary. If the URL points to a local file, or a valid cached copy of the object exists, the object is not copied. Return a tuple (filename, headers)where filename is the local file name under which the object can be found, and headers is whatever the info() method of the object returned by urlopen() returned (for a remote object, possibly cached). Exceptions are the same as for urlopen(). The second argument, if present, specifies the file location to copy to (if absent, the location will be a tempfile with a generated name). The third argument, if present, is a hook function that will be called once on establishment of the network connection and once after each block read thereafter. The hook will be passed three arguments; a count of blocks transferred so far, a block size in bytes, and the total size of the file. The third argument may be -1 on older FTP servers which do not return a file size in response to a retrieval request.

它没有提到默认路径是什么.我想知道我是否总是需要在第二个参数中使用绝对路径?

it doesn't mention what is the default path. I wonder if I always need to use absolute path in the second argument?

推荐答案

默认情况下,如果不提供任何路径,该文件将位于当前目录中. 而且,如果要将文件放置在指定位置,则应始终使用相对路径而不是绝对路径,因为代码可能在不同的os系统上运行.

By default,if you don't give any path, the file will be in your current directory. And if your want to put the file in the specify location,you should always use the relative path instead of absolute path because your codes may run on different os system.

这篇关于python urlretrieve通过HTTP下载文件的默认路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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