Python-比较本地和远程两个文件的上次修改日期

Python-比较本地和远程两个文件的上次修改日期,python,Python,我正在尝试实现一个python脚本,该脚本将比较本地和远程托管文件的最后修改日期 如果远程文件较新,则应: -删除本地文件 -下载上次修改日期完整的远程文件 我找到的最接近的答案是,不过我相信这会下载整个文件,因此不会节省太多资源/时间 我想做的只是查看远程文件的标题,而不是下载整个文件,我认为这样会更快 这是我当前的代码,非常混乱和无趣(参见字符串替换等),我相信有更好/更快的方法-你能建议什么 remote_source = 'http://example.com/somefile.

我正在尝试实现一个python脚本,该脚本将比较本地和远程托管文件的最后修改日期

如果远程文件较新,则应: -删除本地文件 -下载上次修改日期完整的远程文件

我找到的最接近的答案是,不过我相信这会下载整个文件,因此不会节省太多资源/时间

我想做的只是查看远程文件的标题,而不是下载整个文件,我认为这样会更快

这是我当前的代码,非常混乱和无趣(参见字符串替换等),我相信有更好/更快的方法-你能建议什么

    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))
remote\u source='1〕http://example.com/somefile.xml'
local_source='path/to/myfile.xml'
如果路径存在(本地源):
local\u source\u last\u modified=os.path.getmtime(local\u source)
local\u source\u last\u modified=datetime.datetime.fromtimestamp(local\u source\u last\u modified).strftime(“(%Y,%m,%d,%H,%m,%S)”
conn=urllib.urlopen(远程源)
远程源上次修改=conn.info().getdate('last-modified')
远程源上次修改=str(远程源上次修改)
远程源上次修改=远程源上次修改。替换(“,0,1,0)”,“”)
如果本地源上次修改<远程源上次修改:
通过
其他:
headers=urlretrieve(远程源、本地源)[1]
lmStr=headers.getheader(“上次修改”)
远程\u源\u上次\u修改=mktime(strtime(lmStr,“%a,%d%b%Y%H:%M:%S GMT”))
os.utime(本地\源,(远程\源\上次\修改,远程\源\上次\修改))
其他:
headers=urlretrieve(远程源、本地源)[1]
lmStr=headers.getheader(“上次修改”)
远程\u源\u上次\u修改=mktime(strtime(lmStr,“%a,%d%b%Y%H:%M:%S GMT”))
os.utime(本地\源,(远程\源\上次\修改,远程\源\上次\修改))

万一有人读到这篇文章,我的结论如下:

def syncCheck(file_path):
    remote_source = 'http://example.com/' + os.path.basename(file_path)
    local_source = file_path

    headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36'}
    response = requests.head(remote_source, headers = headers)
    remote_source_last_modified = response.headers["last-modified"]
    remote_source_last_modified = time.mktime(datetime.datetime.strptime(remote_source_last_modified[:-4], "%a, %d %b %Y %H:%M:%S").timetuple())

    try:
        if os.path.exists(local_source):
            local_source_last_modified = os.path.getmtime(local_source)
            if local_source_last_modified == remote_source_last_modified:
                break
            else:
                try:
                    os.remove(local_source)
                except:
                    break
                urlretrieve(remote_source, local_source)
                os.utime(local_source, (remote_source_last_modified, remote_source_last_modified))

    else:
        urlretrieve(remote_source, local_source)
        os.utime(local_source, (remote_source_last_modified, remote_source_last_modified))

    except HTTPError, e:
        print("HTTP Error: " + str(e.fp.read()))
    except URLError, e:
        print("URL Error: " + str(e.reason))