简体   繁体   中英

python - how to move only updated files from folder to folder?

I have my source dir and destination dir, containing 5 DLLs. The thing I wanted to do is : If the DLL is not updated then replace the DLL if not - skip

the problem is that I don't know how to check if the DLL is updated.. for example if in the dest dir my DLL is "9/22/2013 15:15" and in my source it's "9/22/2012 16:00" then I want it to be replaced

The code I'm using to update is :

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

I would suggest the same as msw has suggested, that you use os.stat .

Ideally, you can check which objects have been modified recently, checking the st_mtime values of the dll files in your source folder, and your destination folder and seeing which one was greater. If the st_mtime is for your source folder files are greater than your destination folder files, then you move them, otherwise you can ignore them.

This answer assumes that your python file is at the same directory level as your src and dest directory. The logic behind the code below should show you how to update your files:

Root Directory

Root
|
├───dest
├───src
└───updater.py

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

I hope this explains everything. You can get my exact same config from here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM