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

标签 python

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

如果远程文件较新,它应该: - 删除本地文件 - 下载远程文件,并保留最后修改日期

我找到的最接近的答案是 Last Modified of file downloaded does not match its HTTP header ,但是我相信这会下载整个文件,因此不会节省太多资源/时间

我想做的只是查看远程文件的 header ,而不是下载整个文件,我认为这应该更快。

这是我当前的代码,非常困惑和新手(请参阅字符串替换等)我确信有更好/更快的方法 - 你有什么建议?

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

最佳答案

以防万一有人读到这篇文章,这就是我的结论:

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

关于Python - 比较本地和远程两个文件的最后修改日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40714251/

相关文章:

Python - 安装后 pip 可执行文件在哪里?

python - 在元素和属性中搜索字符串

python - 无法读取使用不同方法写入的文件的内容

python - 未安装错误 : TfidfVectorizer - Vocabulary wasn't fitted

python - 如何使用 Python 插件 reCaptcha 客户端进行验证?

python - 在 Python 2.6 中访问外部作用域

python - 为什么 'is' 运算符在算术相等表达式中表现异常

Python + Postgres + psycopg2 : Composing a dynamic json query and dealing with quotes

python - conda 创建环境。参数选择

python - 在 basemap 中绘制数据点并跳过零值