問題

我正在從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()
 

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

  相同標籤的其他問題

pythonurllib