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

标签 python urllib

我正在从网络服务器下载整个目录。它工作正常,但我不知道如何在下载之前获取文件大小以比较它是否在服务器上更新。这可以像我从 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()

@Jon:感谢您的快速回答。它可以工作,但 Web 服务器上的文件大小略小于下载文件的文件大小。

例子:

Local Size  Server Size
 2.223.533  2.115.516
   664.603    662.121

跟CR/LF转换有关系吗?

最佳答案

我已经复制了你所看到的:

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")

关于python - 在 Python 中下载之前获取文件的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5909/

相关文章:

python - Seaborn猫图: change position on x axis

Python urllib 无法打开 localhost

python - 如何在单个列表中获取结果?

python - 循环遍历二维列表 python 不会正确更改值

python - 当链接将重定向到另一个时如何通过 urllib 获取内容?

python - 尽管 mechanize 可以工作,但 urllib.urlopen 不适用于此 url

python - python 3.4.3 中 urllib.httperror 的语法错误

javascript - 如何提交新 Google 帐户请求?

python - 如何生成一天的时间戳?

python - 如何使用 matplotlib 在 y 轴中包含负值?