1
ローカルとリモートでホストされているファイルの最終更新日を比較するPythonスクリプトを実装しようとしています。Python - ローカルとリモートの2つのファイルの最終更新日を比較します。
リモートファイルはそれが必要新しい場合: - ローカルファイル を削除 - そのまま
最終更新日時とリモートファイルをダウンロードし、私はこれを見つけた最も近い答えがLast Modified of file downloaded does not match its HTTP headerである、しかし、私は信じていますこれはファイル全体をダウンロードするので、多くのリソース/時間を節約しません。
私がしたいのは、ファイル全体をダウンロードするのではなく、もっと速くすべきであると思うファイルを見直すことです。
私の現在のコードは非常に乱雑でnoobishです(文字列の置換などを参照してください)私は確かに、より良い/より速い方法です - あなたは何を提案できますか?
remote_source = 'http://example.com/somefile.xml'
local_source = 'path/to/myfile.xml'
if path.exists(local_source):
local_source_last_modified = os.path.getmtime(local_source)
local_source_last_modified = datetime.datetime.fromtimestamp(local_source_last_modified).strftime('(%Y, %m, %d, %H, %M, %S)')
conn = urllib.urlopen(remote_source)
remote_source_last_modified = conn.info().getdate('last-modified')
remote_source_last_modified = str(remote_source_last_modified)
remote_source_last_modified = remote_source_last_modified.replace(", 0, 1, 0)", ")")
if local_source_last_modified < remote_source_last_modified:
pass
else:
headers = urlretrieve(remote_source, local_source)[1]
lmStr = headers.getheader("Last-Modified")
remote_source_last_modified = mktime(strptime(lmStr, "%a, %d %b %Y %H:%M:%S GMT"))
os.utime(local_source, (remote_source_last_modified, remote_source_last_modified))
else:
headers = urlretrieve(remote_source, local_source)[1]
lmStr = headers.getheader("Last-Modified")
remote_source_last_modified = mktime(strptime(lmStr, "%a, %d %b %Y %H:%M:%S GMT"))
os.utime(local_source, (remote_source_last_modified, remote_source_last_modified))