python-如何将仅更新的文件从一个文件夹移动到另一个文件夹?

python-如何将仅更新的文件从一个文件夹移动到另一个文件夹?,python,dll,replace,updates,Python,Dll,Replace,Updates,我有我的源目录和目标目录,包含5个DLL。 我想做的是: 如果DLL未更新,则更换DLL 如果没有-跳过 问题是我不知道如何检查DLL是否更新。。例如,如果在dest dir中,我的DLL是“9/22/2013 15:15”,而在我的源代码中,它是“9/22/2012 16:00”,那么我希望它被替换 我用来更新的代码是: import shutil src = "C:\\steve_test\\Test_xp\\added" dst = "C:\\steve_test\\Test_xp\\mo

我有我的源目录和目标目录,包含5个DLL。 我想做的是: 如果DLL未更新,则更换DLL 如果没有-跳过

问题是我不知道如何检查DLL是否更新。。例如,如果在dest dir中,我的DLL是“9/22/2013 15:15”,而在我的源代码中,它是“9/22/2012 16:00”,那么我希望它被替换

我用来更新的代码是:

import shutil
src = "C:\\steve_test\\Test_xp\\added"
dst = "C:\\steve_test\\Test_xp\\moved"
shutil.move(src, dst)

我建议您使用
os.stat
,这与msw的建议相同

理想情况下,您可以检查最近修改了哪些对象,检查源文件夹和目标文件夹中
dll
文件的
st_mtime
值,并查看哪个值更大。如果源文件夹文件大于目标文件夹文件,则移动它们,否则可以忽略它们

这个答案假设python文件与
src
dest
目录位于同一目录级别。下面代码背后的逻辑应该告诉您如何更新文件:

根目录 updater.py
我希望这能解释一切

你试过什么?您可能会发现它很有用。我编写的代码就是我尝试的代码,但它给了我一个错误,第二次告诉我该文件已经存在
Root
|
├───dest
├───src
└───updater.py
import os
import shutil

# Imported for convenience
from collections import namedtuple

# Main function, so this can act like a script
if __name__ == '__main__':
    ROOT_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__)))  # Root directory

    file_change_times_dest = []  # List of files from destination folder
    file_change_times_src = []  # List of files from source folder

    # Nameed tuple to ease writing code
    FileInformation = namedtuple('FileInformation', ['file_path', 'file_name', 'last_modified'])

    # Loop through files in destination folder to collect information
    for dirpath, dirs, files in os.walk(os.path.join(ROOT_DIR, 'dest')):
        for file in files:
            # getting file path
            file_path = os.path.join(dirpath, file)
            # getting file change info and casting it to FileInformation type
            file_change_times_dest.append(FileInformation(file_path, file, os.stat(file_path).st_mtime))

    # Loop through source folder, same logic
    for dirpath, dirs, files in os.walk(os.path.join(ROOT_DIR, 'src')):
        for file in files:
            file_path = os.path.join(dirpath, file)
            file_change_times_src.append(FileInformation(file_path, file,os.stat(file_path).st_mtime))

    # Comparing the two, using Zip to combine the two lists into a tuple
    for file_comp in zip(file_change_times_dest, file_change_times_src):

        # Settings variables for 0 and 1 to make writing code easier
        _DEST = 0
        _SRC = 1

        # File comparison, to see if file name is the same, since we want to update
        if file_comp[_SRC].file_name == file_comp[_DEST].file_name:
            # If the last modified is greater for source, then we copy
            if file_comp[_SRC].last_modified > file_comp[_DEST].last_modified:
                shutil.copy(file_comp[_SRC].file_path, file_comp[_DEST].file_path)
                print("File moved")  # Just for checking