我正在尝试根据存储在远程HTTP服务器上与存储在本地计算机上的大小和上次修改的时间戳比较远程文件与本地文件。远程文件不经常更改,因此与下载相比,可以进行快速检查。我知道远程文件没有更改,但是当我在本地编写它们然后重新比较时,它们显示了不同的文件大小和时间戳。如何确保我进行了正确的比较并正确写入了文件?
下面的代码已汇总,仅显示了检查的一般过程。
# Pull metadata on remote files
r = requests.head(url)
remote_file_last_modified = r.headers['last-modified']
remote_file_last_modified = int(time.mktime(datetime.datetime.strptime(remote_file_last_modified[:-4], "%a, %d %b %Y %H:%M:%S").timetuple()))
remote_file_size = int(r.headers['content-length'])
# Get metadata on local files
local_file_last_modified = int(os.path.getmtime(local_file_name))
local_file_size = os.stat(local_file_name).st_size
if remote_file_size != local_file_size or remote_file_last_modified != local_file_last_modified:
# They are not the same
# Download remote files
r = requests.get(url)
remote_file_last_modified = r.headers['last-modified']
remote_file_last_modified = int(time.mktime(datetime.datetime.strptime(remote_file_last_modified[:-4], "%a, %d %b %Y %H:%M:%S").timetuple()))
# This is how the remote file is written locally
with open(local_file_name, 'wb') as f:
f.write(r.content)
# Test remote file was written correctly
remote_file_size = int(r.headers['content-length'])
local_file_size = os.stat(local_file_name).st_size
if remote_file_size != local_file_size:
# Files did not download correctly
else:
# Update the local file times to match the remote file
os.utime(local_file_name,(remote_file_last_modified, remote_file_last_modified))