Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/visual-studio-2010/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在下载Python之前获取文件的大小_Python_Urllib - Fatal编程技术网

在下载Python之前获取文件的大小

在下载Python之前获取文件的大小,python,urllib,Python,Urllib,我正在从web服务器下载整个目录。它工作正常,但我不知道如何在下载之前获得文件大小,以比较它是否在服务器上更新。这可以像从FTP服务器下载文件一样完成吗 import urllib import re url = "http://www.someurl.com" # Download the page locally f = urllib.urlopen(url) html = f.read() f.close() f = open ("temp.htm", "w") f.write (h

我正在从web服务器下载整个目录。它工作正常,但我不知道如何在下载之前获得文件大小,以比较它是否在服务器上更新。这可以像从FTP服务器下载文件一样完成吗

import urllib
import re

url = "http://www.someurl.com"

# Download the page locally
f = urllib.urlopen(url)
html = f.read()
f.close()

f = open ("temp.htm", "w")
f.write (html)
f.close()

# List only the .TXT / .ZIP files
fnames = re.findall('^.*<a href="(\w+(?:\.txt|.zip)?)".*$', html, re.MULTILINE)

for fname in fnames:
    print fname, "..."

    f = urllib.urlopen(url + "/" + fname)

    #### Here I want to check the filesize to download or not #### 
    file = f.read()
    f.close()

    f = open (fname, "w")
    f.write (file)
    f.close()

它与CR/LF转换有关吗?

文件大小作为内容长度头发送。以下是如何使用urllib获取它:

>>> site = urllib.urlopen("http://python.org")
>>> meta = site.info()
>>> print meta.getheaders("Content-Length")
['16535']
>>>

使用返回的urllib对象方法
info()
,可以获得有关检索文档的各种信息。抓取当前Google徽标的示例:

>>> import urllib
>>> d = urllib.urlopen("http://www.google.co.uk/logos/olympics08_opening.gif")
>>> print d.info()

Content-Type: image/gif
Last-Modified: Thu, 07 Aug 2008 16:20:19 GMT  
Expires: Sun, 17 Jan 2038 19:14:07 GMT 
Cache-Control: public 
Date: Fri, 08 Aug 2008 13:40:41 GMT 
Server: gws 
Content-Length: 20172 
Connection: Close
这是一个dict,所以要获得文件的大小,需要执行
urlibobject.info()['Content-Length']

print f.info()['Content-Length']
要获取本地文件的大小(用于比较),可以使用os.stat()命令:


另外,如果您连接的服务器支持它,请查看和和标题


使用这些将利用Web服务器的缓存规则,并在内容未更改时返回状态代码。

我已复制了您看到的内容:

import urllib, os
link = "http://python.org"
print "opening url:", link
site = urllib.urlopen(link)
meta = site.info()
print "Content-Length:", meta.getheaders("Content-Length")[0]

f = open("out.txt", "r")
print "File on disk:",len(f.read())
f.close()


f = open("out.txt", "w")
f.write(site.read())
site.close()
f.close()

f = open("out.txt", "r")
print "File on disk after download:",len(f.read())
f.close()

print "os.stat().st_size returns:", os.stat("out.txt").st_size
输出如下:

opening url: http://python.org
Content-Length: 16535
File on disk: 16535
File on disk after download: 16535
os.stat().st_size returns: 16861
我做错了什么?os.stat().st_size是否返回了正确的大小


编辑: 好的,我知道问题出在哪里了:

import urllib, os
link = "http://python.org"
print "opening url:", link
site = urllib.urlopen(link)
meta = site.info()
print "Content-Length:", meta.getheaders("Content-Length")[0]

f = open("out.txt", "rb")
print "File on disk:",len(f.read())
f.close()


f = open("out.txt", "wb")
f.write(site.read())
site.close()
f.close()

f = open("out.txt", "rb")
print "File on disk after download:",len(f.read())
f.close()

print "os.stat().st_size returns:", os.stat("out.txt").st_size
这将产生:

$ python test.py
opening url: http://python.org
Content-Length: 16535
File on disk: 16535
File on disk after download: 16535
os.stat().st_size returns: 16535
确保打开这两个文件进行二进制读/写

// open for binary write
open(filename, "wb")
// open for binary read
open(filename, "rb")
在Python3中:

>>> import urllib.request
>>> site = urllib.request.urlopen("http://python.org")
>>> print("FileSize: ", site.length)
使用HEAD而不是GET的基于解决方案(还打印HTTP头):

#/usr/bin/python
#显示远程文件的大小而不下载
来自未来导入打印功能
导入系统
导入请求
#兆字节中的字节数
MBFACTOR=float(1对于python3(在3.5上测试)方法,我建议:

with urlopen(file_url) as in_file, open(local_file_address, 'wb') as out_file:
    print(in_file.getheader('Content-Length'))
    out_file.write(response.read())

@关于本地/服务器文件大小差异的PabloG

以下是可能发生的原因的高级说明性解释:

磁盘上的大小有时与数据的实际大小不同。 它取决于底层文件系统及其对数据的操作方式。 正如您在Windows中看到的,格式化闪存驱动器时,要求您提供“块/群集大小”,并且大小不同[512b-8kb]。 当文件写入磁盘时,它被存储在磁盘块的“链接列表”中。 当某个块用于存储文件的一部分时,其他文件内容不会存储在同一个blok中,因此即使该块没有占用整个块空间,该块也会被其他文件呈现为不可用

例如: 当文件系统被划分为512b块时,我们需要存储600b文件,两个块将被占用。第一个块将被完全利用,而第二个块将只利用88b,剩余的(512-88)b将不可用,导致“磁盘上的文件大小”为1024b。 这就是为什么Windows对“文件大小”和“磁盘大小”有不同的标记

注:
较小/较大的FS块有不同的优点和缺点,因此在使用文件系统之前,请进行更好的研究。

可能。您可以在其上运行diff并查看差异吗?您还可以查看二进制文件(.zip)中的文件大小差异吗文件?编辑:这是像Etags这样的东西派上用场的地方。服务器会告诉你什么时候发生了变化,所以你不必下载完整的文件来找出它。你是对的,我在打开本地文件进行写作时没有使用“wb”。工作起来很有魅力!THX当你这样做时
site=urllib.urlopen(link)
您已经执行了文件下载,因此在下载文件之前,它的大小不是文件的大小,而是下载到从中检索内容的缓冲区中-length@Ciastopiekarz我想是当你尝试阅读的时候在缓冲区
urllib.urlopen
中实际下载的文件在至少3.6中不再有效。此下载文件!我一直在使用此解决方案,但我遇到了一个边缘情况,其中有时未定义内容长度头。有人能解释为什么不能一致地返回它吗?也许可以解释一下?
>>> import urllib.request
>>> site = urllib.request.urlopen("http://python.org")
>>> print("FileSize: ", site.length)
$ python filesize-remote-url.py https://httpbin.org/image/jpeg
...
Content-Length                          : 35588
FILE SIZE (MB)                          : 0.03 MB
with urlopen(file_url) as in_file, open(local_file_address, 'wb') as out_file:
    print(in_file.getheader('Content-Length'))
    out_file.write(response.read())