def _preserve_timestamps(file_entry, output_path): """Obtain and set (to preserve) original timestamps of exported files.""" stat_object = file_entry.GetStat() if os.name == WINDOWS_IDENTIFIER: accessed = created = modified = dt.now() if stat_object.atime: if stat_object.atime_nano: accessed = dt.fromtimestamp((float(str(stat_object.atime) + '.' + str(stat_object.atime_nano)))) else: accessed = dt.fromtimestamp(stat_object.atime) if stat_object.crtime: if stat_object.crtime_nano: created = dt.fromtimestamp((float(str(stat_object.crtime) + '.' + str(stat_object.crtime_nano)))) else: created = dt.fromtimestamp(stat_object.crtime) if stat_object.mtime: if stat_object.mtime_nano: modified = dt.fromtimestamp((float(str(stat_object.mtime) + '.' + str(stat_object.mtime_nano)))) else: modified = dt.fromtimestamp(stat_object.mtime) handle = CreateFileW(output_path, GENERIC_WRITE, FILE_SHARE_WRITE, None, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, None) SetFileTime(handle, created, accessed, modified) # does not seem to preserve nano precision of timestamps CloseHandle(handle) else: os.utime(output_path, (stat_object.atime, stat_object.mtime))
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 set_mp4_timestamp(path): '''用mvhd中的creation_time和modification_time设置mp4文件的创建时间和修改时间。 如果目标时间与当前文件的创建时间一致(时间差小于1s)则不修改。 如果creation_time和modification_time之中有1个为0,则用另一个赋值,都为零则不修改文件时间。 ''' try: mvhd = get_mvhd(path) except ValueError as error: print(f'When processing {path} a error occurs: {error}. Skip it.') return # mvhd is FullBox,是Box的扩展,Box结构的基础上在Header中增加8bits version和24bits flags try: version = mvhd[8:9] if version == b'\x00': creation_time = struct.unpack('>I', mvhd[12:16])[0] modification_time = struct.unpack('>I', mvhd[16:20])[0] elif version == b'\x01': creation_time = struct.unpack('>Q', mvhd[12:20])[0] modification_time = struct.unpack('>Q', mvhd[20:28])[0] except (IndexError, struct.error): print( f'{path} contains no valid mvhd box. May not be a valid mp4 file. Skip it.' ) return if creation_time + modification_time == 0: print( f'{path} contains no creation_time or modification_time. Skip it.') return if creation_time == 0: creation_time = modification_time if modification_time == 0: modification_time = creation_time new_ctimestamp = time.mktime(get_datetime(creation_time).timetuple()) new_mtimestamp = time.mktime(get_datetime(modification_time).timetuple()) mod_flag = False old_mtimestamp = getmtime(path) mtime = Time(old_mtimestamp) if abs(old_mtimestamp - new_mtimestamp) >= 1: mtime = Time(new_mtimestamp) mod_flag = True old_ctimestamp = getctime(path) ctime = Time(old_ctimestamp) if abs(old_ctimestamp - new_ctimestamp) >= 1: ctime = Time(new_ctimestamp) mod_flag = True if mod_flag: handle = CreateFile(path, GENERIC_WRITE, FILE_SHARE_WRITE, None, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, None) SetFileTime(handle, ctime, None, mtime) CloseHandle(handle)
def mod_ctime(path): """ increments file creation time by a nanosecond parameters: string path, int mod_size | return: none """ ctime = os.path.getctime(path) + 0.001 # increment ctime by 0.001 fh = CreateFile(path, GENERIC_WRITE, FILE_SHARE_WRITE, None, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0) SetFileTime(fh, datetime.datetime.fromtimestamp(ctime), None, None) # update file ctime CloseHandle(fh)
def create_file(file_name, c_file, time, a_path, ftp): current_file = open(file_name, "wb") ftp.retrbinary("RETR " + a_path + "/" + c_file, current_file.write) print('\tAdded:', file_name) current_file.close() win_file = CreateFile( file_name, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, None, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, None) # noinspection PyUnresolvedReferences win_time = Time(time) SetFileTime(win_file, win_time, None, None) CloseHandle(win_file)
def change_f_time(f, f_create_time, f_access_time, f_modify_time): """ 修改文件或文件夹的时间 f: 文件/文件夹的路径 f_create_time, f_access_time, f_modify_time: 文件/文件夹的创建时间、访问时间、修改时间 """ f_type = 0 if os.path.isfile(f) else FILE_FLAG_BACKUP_SEMANTICS file_handle = CreateFile(f, GENERIC_READ | GENERIC_WRITE, 0, None, OPEN_EXISTING, f_type, 0) # todo: f_type=0表示创建文件,=FILE_FLAG_BACKUP_SEMANTICS 表示创建文件夹! f_create_time = Time(f_create_time) f_access_time = f_create_time f_modify_time = Time(f_modify_time) SetFileTime(file_handle, f_create_time, f_access_time, f_modify_time) # createTimes, accessTimes, modifyTimes CloseHandle(file_handle)
def set_time(path, ctime=None, atime=None, mtime=None): """ changes file times to input times parameters: string path, int ctime | return: none """ # get datetime value of time if time has a value if ctime != None: ctime = datetime.datetime.fromtimestamp(ctime) if atime != None: atime = datetime.datetime.fromtimestamp(atime) if mtime != None: mtime = datetime.datetime.fromtimestamp(mtime) fh = CreateFile(path, GENERIC_WRITE, FILE_SHARE_WRITE, None, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0) SetFileTime(fh, ctime, atime, mtime) # update file times (None value keeps previous value) CloseHandle(fh)
def randomizeFileTime( file ): #uses os.utime to set a+m times and pywin_cstructs to set file creation times name = os.path.basename(file) rd = getRandomDate() at = random.randint(200000000, 1000050000) mt = random.randint(200000000, 1000050000) os.utime(file, (at, mt)) ctimeform = "%d.%m.%Y %H:%M:%S" off = 0 ct = time.localtime(time.mktime(time.strptime( rd, ctimeform))) #prepares time in format specified tmp = CreateFile(file, GENERIC_READ | GENERIC_WRITE, 0, None, OPEN_EXISTING, 0, 0) newct = Time(time.mktime(ct)) SetFileTime(tmp, newct) CloseHandle(tmp) print(name)
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 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 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)
def exact(filePath): starttime = time.time() unZf = zipfile.ZipFile(filePath, 'r') shutil.rmtree(myconfig['save_path'],ignore_errors=True) time.sleep(0.1) # os.makedirs(save_folder, exist_ok=True) # unZf.extractall(path=save_folder,members=unZf.namelist()) for name in unZf.namelist(): unZfTarge = os.path.join(save_folder, name) dirname = os.path.dirname(unZfTarge) os.makedirs(dirname, exist_ok=True) with open(unZfTarge, 'wb') as f: f.write(unZf.read(name)) fh = CreateFile(unZfTarge, GENERIC_READ | GENERIC_WRITE, 0, None, OPEN_EXISTING, 0, 0) #伪造写入时间 createtime = unZf.getinfo(name).date_time time_formated = time.strptime(str(createtime), "(%Y, %m, %d, %H, %M, %S)") time_win = Time(time.mktime(time_formated)) SetFileTime(fh,time_win,time_win,time_win) unZf.close() print(f"解压用时{time.time() - starttime}")
def modify_file_all_time(file_path, create_time): """ 用来修改任意文件的相关时间属性,时间格式:YYYY-MM-DD HH:MM:SS 例如:2019-02-02 00:01:02 :param file_path: 文件路径名 :param create_time: 创建时间 :param modifyTime: 修改时间 :param accessTime: 访问时间 :param offset: 时间偏移的秒数,tuple格式,顺序和参数时间对应 """ print(file_path, create_time) try: fh = CreateFile(file_path, GENERIC_READ | GENERIC_WRITE, 0, None, OPEN_EXISTING, 0, 0) final_create_time = Time(time.mktime(create_time)) final_access_time = Time(time.mktime(create_time)) final_modify_time = Time(time.mktime(create_time)) SetFileTime(fh, final_create_time, final_access_time, final_modify_time) CloseHandle(fh) return 0 except Exception as e: print(e) return 1
def set_jpg_timestamp(path): '''用保存在EXIF中的拍摄时间设置图片的创建时间和修改时间。 如果目标时间与当前文件的创建时间一致(时间差小于1s)则不修改。 ''' f = open(path, 'rb') tags = exifread.process_file(f, details=False) f.close() # new_ctimestamp1 for timestamp found in file tags, new_ctimestamp2 for timestamp found in file name. new_ctimestamp1 = None new_ctimestamp2 = None file_name = split(path)[1] print(f' [{file_name}]') # 尝试获取'Image DateTime'的时间 try: new_ctimestamp1 = time.mktime(time.strptime( tags['Image DateTime'].values, '%Y:%m:%d %H:%M:%S')) print(' └─Found "Image DateTime"', end='') except KeyError: print(' └─"Image DateTime" not found, try next method.') except ValueError: print(' └─"Image DateTime" is not valid, try next method.') # 尝试获取'EXIF DateTimeOriginal'的时间 if not new_ctimestamp1: try: new_ctimestamp1 = time.mktime(time.strptime( tags['EXIF DateTimeOriginal'].values, '%Y:%m:%d %H:%M:%S')) print(' └─Found "EXIF DateTimeOriginal"', end='') except KeyError: print(' └─"EXIF DateTimeOriginal" not found, try next method.') except ValueError: print(' └─"EXIF DateTimeOriginal" is not valid, try next method.') # 尝试获取'EXIF DateTimeDigitized'的时间 if not new_ctimestamp1: try: new_ctimestamp1 = time.mktime(time.strptime( tags['EXIF DateTimeDigitized'].values, '%Y:%m:%d %H:%M:%S')) print(' └─Found "EXIF DateTimeDigitized"', end='') except KeyError: print(' └─"EXIF DateTimeDigitized" not found, try next method.') except ValueError: print(' └─"EXIF DateTimeDigitized" is not valid, try next method.') # 尝试从文件名获取时间,文件名需形如'XXX_YYYYmmdd_HHMMSS(_XXXXX).jp(e)g' file_name_no_ext = splitext(split(path)[1])[0] try: time_strs = file_name_no_ext.split('_')[1:3] new_ctimestamp2 = time.mktime(time.strptime( ''.join(time_strs), '%Y%m%d%H%M%S')) if new_ctimestamp1: print(' and file name contains timestamp', end='') else: print(' └─File name contains timestamp', end='') except (ValueError, OverflowError): if not new_ctimestamp1: print( ' └─File name dose not match XXX_YYYYmmdd_HHMMSS(_XXXXX).jp(e)g format.') if new_ctimestamp1 or new_ctimestamp2: if new_ctimestamp1 is None: new_ctimestamp = new_ctimestamp2 elif new_ctimestamp2 is None: new_ctimestamp = new_ctimestamp1 else: new_ctimestamp = new_ctimestamp1 if new_ctimestamp1 < new_ctimestamp2 else new_ctimestamp2 old_ctimestamp = getctime(path) if abs(old_ctimestamp - new_ctimestamp) >= 1: ctime = Time(new_ctimestamp) try: handle = CreateFile(path, GENERIC_WRITE, FILE_SHARE_WRITE, None, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, None) SetFileTime(handle, ctime, None, ctime) CloseHandle(handle) print('. Done.') except: print( '. Can not change ctime and mtime. Make sure you have right permission. Skip it.') else: print('. No need to change. Skip it.') else: print(' └─No valid timestamp found. Skip it.')
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)
# 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 ctime = time.strftime(format, time.localtime(os.path.getctime(fName))) mtime = time.strftime(format, time.localtime(os.path.getmtime(fName))) atime = time.strftime(format, time.localtime(os.path.getatime(fName))) print("CHECK MODIFICATION:") print("FileName: %s" % fName) print("Create : %s" % (ctime)) print("Modify : %s" % (mtime)) print("Access : %s" % (atime)) # from zhihu
tz = pytz.timezone(args.timezone) shutil.copy2(source, dest) if os.path.isdir(dest): dest_file = os.path.join(dest, src_file_name) else: dest_file = dest created = dt.fromtimestamp(os.path.getctime(source)) created = Time(tz.localize(created)) modified = dt.fromtimestamp(os.path.getmtime(source)) modified = Time(tz.localize(modified)) accessed = dt.fromtimestamp(os.path.getatime(source)) accessed = Time(tz.localize(accessed)) print("Source\n======") print("Created: {}\nModified: {}\nAccessed: {}".format( created, modified, accessed)) handle = CreateFile(dest_file, GENERIC_WRITE, FILE_SHARE_WRITE, None, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, None) SetFileTime(handle, created, accessed, modified) CloseHandle(handle) created = tz.localize(dt.fromtimestamp(os.path.getctime(dest_file))) modified = tz.localize(dt.fromtimestamp(os.path.getmtime(dest_file))) accessed = tz.localize(dt.fromtimestamp(os.path.getatime(dest_file))) print("\nDestination\n===========") print("Created: {}\nModified: {}\nAccessed: {}".format( created, modified, accessed))