文件夹拷贝,保留文件修改时间, 按修改时间最新排序

import sys, os, time, shutil
from stat import ST_ATIME, ST_CTIME, ST_MTIME
from win32file import CopyFile

def test(self):
    remoteDir = "\\\\orc-fs\\Builds\\FP\\builds01\\flair\\Main"
    localDir = "d:\\Main"
    i = 0
    for name in os.listdir(remoteDir):
        print name
        if i == 3:
            break
        shutil.copytree(os.path.join(remoteDir, name), os.path.join(localDir, name), True)
        try:
            stat1 = os.stat(os.path.join(remoteDir, name))
            print stat1
            print time.ctime(stat1[ST_CTIME]), time.ctime(stat1[ST_MTIME])
        except os.error:
            sys.stderr.write("Error")
            sys.exit(1)
        try:
            os.utime(os.path.join(localDir, name), (stat1[ST_CTIME], stat1[ST_MTIME]))
            stat2 = os.stat(os.path.join(localDir, name))
            os.path.getctime(os.path.join(localDir, name))
            print time.ctime(stat2[ST_CTIME]), time.ctime(stat2[ST_MTIME])
        except os.error:
            sys.stderr.write("Error")
            sys.exit(2)
        if stat1[ST_CTIME] == stat2[ST_CTIME] and stat1[ST_MTIME] == stat2[ST_MTIME]:
            print "Good"
        i += 1
def compare(self):
    remoteDir = "\\\\orc-fs\\Builds\\FP\\builds01\\flair\\Main"
    localDir = "d:\\Main"
    files = [(os.path.getmtime(os.path.join(remoteDir, x)) , os.path.join(remoteDir, x)) for x in os.listdir(remoteDir)]
    files.sort()
    files.reverse()
    for item in files:
        print item

if __name__ == "__main__":
    # test(1)
    compare(1)

原文地址:https://www.cnblogs.com/shenfei2031/p/2132735.html