def modifyFileTime(filePath, createTime, modifyTime, accessTime, offset): """ 用来修改任意文件的相关时间属性,时间格式:YYYY-MM-DD HH:MM:SS 例如:2019-02-02 00:01:02 :param filePath: 文件路径名 :param createTime: 创建时间 :param modifyTime: 修改时间 :param accessTime: 访问时间 :param offset: 时间偏移的秒数,tuple格式,顺序和参数时间对应 """ try: format = "%Y-%m-%d %H:%M:%S" # 时间格式 cTime_t = timeOffsetAndStruct(createTime, format, offset[0]) mTime_t = timeOffsetAndStruct(modifyTime, format, offset[1]) aTime_t = timeOffsetAndStruct(accessTime, format, offset[2]) fh = CreateFile(filePath, GENERIC_READ | GENERIC_WRITE, 0, None, OPEN_EXISTING, 0, 0) createTimes, accessTimes, modifyTimes = GetFileTime(fh) createTimes = Time(time.mktime(cTime_t)) accessTimes = Time(time.mktime(aTime_t)) modifyTimes = Time(time.mktime(mTime_t)) SetFileTime(fh, createTimes, accessTimes, modifyTimes) CloseHandle(fh) return 0 except: return 1
def modify_file_all_time(filePath, createTime): """ 用来修改任意文件的相关时间属性,时间格式:YYYY-MM-DD HH:MM:SS 例如:2019-02-02 00:01:02 :param filePath: 文件路径名 :param createTime: 创建时间 :param modifyTime: 修改时间 :param accessTime: 访问时间 :param offset: 时间偏移的秒数,tuple格式,顺序和参数时间对应 """ print(filePath, createTime) try: format = "%Y-%m-%d %H:%M:%S" # 时间格式 fh = CreateFile(filePath, GENERIC_READ | GENERIC_WRITE, 0, None, OPEN_EXISTING, 0, 0) createTimes, accessTimes, modifyTimes = GetFileTime(fh) createTimes = Time(time.mktime(createTime)) accessTimes = Time(time.mktime(createTime)) modifyTimes = Time(time.mktime(createTime)) SetFileTime(fh, createTimes, accessTimes, modifyTimes) CloseHandle(fh) return 0 except Exception as e: print(e) return 1
def file_name(file_dir): for root, dirs, files in os.walk(file_dir): for filename in files: fh = CreateFile(filename, GENERIC_READ | GENERIC_WRITE, 0, None, OPEN_EXISTING, 0, 0) createTime, accessTime, modifyTime = GetFileTime(fh) createTime = Time(time.mktime(time.gmtime(random.uniform(1514780000,1546300000)))) accessTime = Time(time.mktime(time.gmtime(random.uniform(1514780000,1546300000)))) modifyTime = Time(time.mktime(time.gmtime(random.uniform(1514780000,1546300000)))) SetFileTime(fh, createTime, accessTime, modifyTime) CloseHandle(fh)
def change_file_time(path, delta): if not os.path.exists(path): log_message("Pfad: " + path + " existiert nicht!", "info") return if platform.system() == "Windows": # modify filetimes on Windows fh = CreateFile(path, GENERIC_WRITE, FILE_SHARE_WRITE, None, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0) cTime, aTime, mTime = GetFileTime(fh) cTime = datetime.fromtimestamp(cTime.timestamp() - delta) aTime = datetime.fromtimestamp(aTime.timestamp() - delta) mTime = datetime.fromtimestamp(mTime.timestamp() - delta) SetFileTime(fh, cTime, aTime, mTime) CloseHandle(fh) else: # modify filetimes on Linux/Mac a_time = os.path.getatime(path) m_time = os.path.getmtime(path) a_time = a_time - delta m_time = m_time - delta os.utime(path, (a_time, m_time))
def build_config(root): curr_path = os.path.dirname(os.path.abspath(os.path.expanduser(__file__))) save_path = os.path.join(curr_path, "save") hasher0 = hashlib.sha1() hasher0.update(root.encode("utf-8")) save_path = os.path.join(save_path, str(hasher0.hexdigest())) if not os.path.exists(save_path): os.mkdir(save_path) save_build_config = os.path.join(save_path, "build_config.h") build_config = os.path.join( root, "mxnet/3rdparty/dmlc-core/include/dmlc/build_config.h") if not os.path.exists(build_config): return if not os.path.exists(save_build_config): copyfile(build_config, save_build_config) hasher1 = hashlib.sha512() with open(build_config, 'rb') as afile: buf = afile.read() hasher1.update(buf) hasher2 = hashlib.sha512() with open(save_build_config, 'rb') as afile: buf = afile.read() hasher2.update(buf) if hasher1.hexdigest() == hasher2.hexdigest(): fh = CreateFile(save_build_config, GENERIC_READ | GENERIC_WRITE, 0, None, OPEN_EXISTING, 0, 0) create_times, access_times, modify_times = GetFileTime(fh) CloseHandle(fh) fh = CreateFile(build_config, GENERIC_READ | GENERIC_WRITE, 0, None, OPEN_EXISTING, 0, 0) SetFileTime(fh, create_times, access_times, modify_times) CloseHandle(fh) else: copyfile(build_config, save_build_config)
def modify_model_file_time(model_file_dir): for root, dirs, files in os.walk(model_file_dir): start_time=1514780000 latest_time=1546300000 for filename in files: if filename!='untitled1.py': fh = CreateFile(filename, GENERIC_READ | GENERIC_WRITE, 0, None, OPEN_EXISTING, 0, 0) createTime, accessTime, modifyTime = GetFileTime(fh) start=random.uniform(start_time,latest_time) createTime = Time(time.mktime(time.gmtime(start))) modify_time=start+random.uniform(0,latest_time-start) modifyTime = Time(time.mktime(time.gmtime(modify_time))) access_time=modify_time+random.uniform(0,latest_time-modify_time) accessTime = Time(time.mktime(time.gmtime(access_time))) SetFileTime(fh, createTime, accessTime, modifyTime) CloseHandle(fh)
cTime_t = time.localtime(time.mktime(time.strptime(cTime, format)) + offset) mTime_t = time.localtime(time.mktime(time.strptime(mTime, format)) + offset) aTime_t = time.localtime(time.mktime(time.strptime(aTime, format)) + offset) # visually check if conversion was ok print() print("FileName: %s" % fName) print("Create : %s --> %s OK" % (cTime, time.strftime(format, cTime_t))) print("Modify : %s --> %s OK" % (mTime, time.strftime(format, mTime_t))) print("Access : %s --> %s OK" % (aTime, time.strftime(format, aTime_t))) print() # change timestamp of file fh = CreateFile(fName, GENERIC_READ | GENERIC_WRITE, 0, None, OPEN_EXISTING, 0, 0) createTime, accessTime, modifyTime = GetFileTime(fh) print("Change Create from", createTime, "to %s" % (time.strftime(format, cTime_t))) print("Change Modify from", modifyTime, "to %s" % (time.strftime(format, mTime_t))) print("Change Access from", accessTime, "to %s" % (time.strftime(format, aTime_t))) print() createTime = Time(time.mktime(cTime_t)) accessTime = Time(time.mktime(aTime_t)) modifyTime = Time(time.mktime(mTime_t)) SetFileTime(fh, createTime, accessTime, modifyTime) CloseHandle(fh) # check if all was ok
def edit_file_list_time(c_time, dir_path, file_name_list): # get arguments # c_time = "08.10.2018 15:34:44" m_time = c_time a_time = c_time time_format = "%d.%m.%Y %H:%M:%S" c_time_stripe = time.strptime(c_time, time_format) m_time_stripe = time.strptime(m_time, time_format) a_time_stripe = time.strptime(a_time, time_format) c_time_second = time.mktime(c_time_stripe) m_time_second = time.mktime(m_time_stripe) a_time_second = time.mktime(a_time_stripe) for fileName in file_name_list: # f_name = "h:\Downloads\待处理文件\20180702.xls" f_name = dir_path + fileName # specify time format offset = random.randint(60, 150) # in seconds # create struct_time object c_time_second = c_time_second + offset m_time_second = m_time_second + offset a_time_second = a_time_second + offset c_time_t = time.localtime(c_time_second) m_time_t = time.localtime(m_time_second) a_time_t = time.localtime(a_time_second) # visually check if conversion was ok print() print("FileName: %s" % f_name) print("Create : %s --> %s OK" % (c_time, time.strftime(time_format, c_time_t))) print("Modify : %s --> %s OK" % (m_time, time.strftime(time_format, m_time_t))) print("Access : %s --> %s OK" % (a_time, time.strftime(time_format, a_time_t))) print() # change timestamp of file fh = CreateFile(f_name, GENERIC_READ | GENERIC_WRITE, 0, None, OPEN_EXISTING, 0, 0) create_time, access_time, modify_time = GetFileTime(fh) print("Change Create from", create_time, "to %s" % (time.strftime(time_format, c_time_t))) print("Change Modify from", modify_time, "to %s" % (time.strftime(time_format, m_time_t))) print("Change Access from", access_time, "to %s" % (time.strftime(time_format, a_time_t))) print() create_time = Time(time.mktime(c_time_t)) access_time = Time(time.mktime(a_time_t)) modify_time = Time(time.mktime(m_time_t)) SetFileTime(fh, create_time, access_time, modify_time) CloseHandle(fh) # check if all was ok ctime = time.strftime(time_format, time.localtime(os.path.getctime(f_name))) mtime = time.strftime(time_format, time.localtime(os.path.getmtime(f_name))) atime = time.strftime(time_format, time.localtime(os.path.getatime(f_name))) print("CHECK MODIFICATION:") print("FileName: %s" % f_name) print("Create : %s" % ctime) print("Modify : %s" % mtime) print("Access : %s" % atime)