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