def testFileTimesTimezones(self): filename = tempfile.mktemp("-testFileTimes") # now() is always returning a timestamp with microseconds but the # file APIs all have zero microseconds, so some comparisons fail. now_utc = win32timezone.utcnow().replace(microsecond=0) now_local = now_utc.astimezone(win32timezone.TimeZoneInfo.local()) h = win32file.CreateFile( filename, win32file.GENERIC_READ | win32file.GENERIC_WRITE, 0, None, win32file.CREATE_ALWAYS, 0, 0, ) try: win32file.SetFileTime(h, now_utc, now_utc, now_utc) ct, at, wt = win32file.GetFileTime(h) self.failUnlessEqual(now_local, ct) self.failUnlessEqual(now_local, at) self.failUnlessEqual(now_local, wt) # and the reverse - set local, check against utc win32file.SetFileTime(h, now_local, now_local, now_local) ct, at, wt = win32file.GetFileTime(h) self.failUnlessEqual(now_utc, ct) self.failUnlessEqual(now_utc, at) self.failUnlessEqual(now_utc, wt) finally: h.close() os.unlink(filename)
def testFileTimesTimezones(self): if not issubclass(pywintypes.TimeType, datetime.datetime): # maybe should report 'skipped', but that's not quite right as # there is nothing you can do to avoid it being skipped! return filename = tempfile.mktemp("-testFileTimes") now_utc = win32timezone.utcnow() now_local = now_utc.astimezone(win32timezone.TimeZoneInfo.local()) h = win32file.CreateFile( filename, win32file.GENERIC_READ | win32file.GENERIC_WRITE, 0, None, win32file.CREATE_ALWAYS, 0, 0) try: win32file.SetFileTime(h, now_utc, now_utc, now_utc) ct, at, wt = win32file.GetFileTime(h) self.failUnlessEqual(now_local, ct) self.failUnlessEqual(now_local, at) self.failUnlessEqual(now_local, wt) # and the reverse - set local, check against utc win32file.SetFileTime(h, now_local, now_local, now_local) ct, at, wt = win32file.GetFileTime(h) self.failUnlessEqual(now_utc, ct) self.failUnlessEqual(now_utc, at) self.failUnlessEqual(now_utc, wt) finally: h.close() os.unlink(filename)
def change_file_creation_time(fname, newtime): winfile = win32file.CreateFile(fname, win32con.GENERIC_WRITE, win32con.FILE_SHARE_READ | win32con.FILE_SHARE_WRITE | win32con.FILE_SHARE_DELETE, None, win32con.OPEN_EXISTING, win32con.FILE_ATTRIBUTE_NORMAL, None) win32file.SetFileTime(winfile, newtime, newtime, newtime) winfile.close()
def ftouch(file, newtime): """Set new **modified** time for given file. Supports Unix and Windows (with win32 installed) :param file: file to be modified :param newtime: new time stamp to be set """ try: import pywintypes, win32file #wintime = pywintypes.Time(newtime[:6]) # must limit to 6 or SetFileTime won't work import win32timezone, datetime localTZ = win32timezone.TimeZoneInfo.local() wintime = datetime.datetime( newtime, localTZ) # this fixes time differences when in DST f = win32file.CreateFile( file, win32file.GENERIC_WRITE, # otherwise creation time not set 0, None, win32file.OPEN_EXISTING, 0, 0) win32file.SetFileTime(f, wintime, wintime, wintime) # win32file.FlushFileBuffers(f) f.Close() except: it = time.mktime(newtime) os.utime(file, (it, it)) # update modification/creation time
def do_download_file(self, base_url: str, media_item: DatabaseMedia): """ Runs in a process pool and does a download of a single media item. """ if self.case_insensitive_fs: relative_folder = str(media_item.relative_folder).lower() filename = str(media_item.filename).lower() else: relative_folder = media_item.relative_folder filename = media_item.filename local_folder = self._root_folder / relative_folder local_full_path = local_folder / filename if media_item.is_video(): download_url = "{}=dv".format(base_url) timeout = self.video_timeout else: download_url = "{}=d".format(base_url) timeout = self.image_timeout temp_file = tempfile.NamedTemporaryFile(dir=local_folder, delete=False) t_path = Path(temp_file.name) try: response = self._session.get(download_url, stream=True, timeout=timeout) response.raise_for_status() shutil.copyfileobj(response.raw, temp_file) temp_file.close() temp_file = None response.close() t_path.rename(local_full_path) create_date = Utils.safe_timestamp(media_item.create_date) os.utime( str(local_full_path), ( Utils.safe_timestamp(media_item.modify_date).timestamp(), create_date.timestamp(), ), ) if _use_win_32: file_handle = win32file.CreateFile( str(local_full_path), win32file.GENERIC_WRITE, 0, None, win32con.OPEN_EXISTING, 0, None, ) win32file.SetFileTime(file_handle, *(create_date, ) * 3) file_handle.close() os.chmod(str(local_full_path), 0o666 & ~self.current_umask) except KeyboardInterrupt: log.debug("User cancelled download thread") raise finally: if temp_file: temp_file.close() if t_path.exists(): t_path.unlink()
def setWinCreationTime(fileName, creationTime): """creationTime - datetime object""" filehandle = win32file.CreateFile(fileName, win32file.GENERIC_WRITE, 0, None, win32file.OPEN_EXISTING, 0, None) theCreationTime = theLastAccessTime = theLastWriteTime = creationTime win32file.SetFileTime(filehandle, theCreationTime, theLastAccessTime, theLastWriteTime)
def setFileDates(fileName, dates): """Sets file modification and creation dates to the specified value""" try: import win32file, win32con filehandle = win32file.CreateFile(fileName, win32file.GENERIC_WRITE, 0, None, win32con.OPEN_EXISTING, 0, None) win32file.SetFileTime(filehandle, *(dates['exif'],)*3) filehandle.close() except: os.utime(fileName, (time.mktime(dates['exif']),)*2)
def change_file_date(self, filename: str, mtime: str = None, ctime: str = None) -> None: """ Change the FS modification and creation dates of a file. Since there is no creation time on GNU/Linux, the ctime will not be taken into account if running on this platform. :param filename: The file to modify :param mtime: The modification time :param ctime: The creation time """ if WINDOWS: filename = safe_long_path(filename) log.trace("Setting file dates for %r (ctime=%r, mtime=%r)", filename, ctime, mtime) # Set the creation time first as on macOS using touch will change ctime and mtime. # The modification time will be updated just after, if needed. if ctime: try: ctime = datetime.fromtimestamp(ctime) except TypeError: ctime = datetime.strptime(ctime, "%Y-%m-%d %H:%M:%S") if MAC: if isinstance(filename, bytes): filename = filename.decode("utf-8") cmd = [ "touch", "-mt", ctime.strftime("%Y%m%d%H%M.%S"), filename ] subprocess.check_call(cmd) elif WINDOWS: winfile = win32file.CreateFile( filename, win32con.GENERIC_WRITE, (win32con.FILE_SHARE_READ | win32con.FILE_SHARE_WRITE | win32con.FILE_SHARE_DELETE), None, win32con.OPEN_EXISTING, win32con.FILE_ATTRIBUTE_NORMAL, None, ) win32file.SetFileTime(winfile, ctime) if mtime: try: mtime = int(mtime) except ValueError: mtime = mktime(strptime(mtime, "%Y-%m-%d %H:%M:%S")) os.utime(filename, (mtime, mtime))
def winddows_touch(file_name,newtime): #os.utime does not work well in windows, changes modified time but not creation time """ All this is needed because windows stores file creation time but linux does not so the other functions do not fix that (they only fix modified and last access date)""" newtime= datetime.datetime.fromtimestamp(newtime) if newtime.tzinfo is None: tz = pytz.timezone('UTC') newtime = tz.localize(newtime) ctime = newtime; atime = newtime; mtime = newtime; filehandle = win32file.CreateFile(file_name, win32file.GENERIC_WRITE,0, None, win32file.OPEN_EXISTING, 0, 0) win32file.SetFileTime(filehandle, ctime, atime, mtime)
def change_file_creation_time(fname, newtime): # https://stackoverflow.com/questions/4996405/how-do-i-change-the-file-creation-date-of-a-windows-file wintime = pywintypes.Time(newtime) winfile = win32file.CreateFile( fname, win32con.GENERIC_WRITE, win32con.FILE_SHARE_READ | win32con.FILE_SHARE_WRITE | win32con.FILE_SHARE_DELETE, None, win32con.OPEN_EXISTING, win32con.FILE_ATTRIBUTE_NORMAL, None) win32file.SetFileTime(winfile, wintime, None, None) winfile.close()
def setFileDates(fileName, dates): """Sets file modification and creation dates to the specified value""" if __use_win_32: filehandle = win32file.CreateFile(fileName, win32file.GENERIC_WRITE, 0, None, win32con.OPEN_EXISTING, 0, None) win32file.SetFileTime(filehandle, *(dates['exif'], ) * 3) filehandle.close() else: os.utime(fileName, (time.mktime(dates['exif'].utctimetuple()), ) * 2)
def changeFileCreationTime(fname, creationtime, modifytime): winctime = pywintypes.Time(creationtime) winmtime = pywintypes.Time(modifytime) winfile = win32file.CreateFile( fname, win32con.GENERIC_WRITE, win32con.FILE_SHARE_READ | win32con.FILE_SHARE_WRITE | win32con.FILE_SHARE_DELETE, None, win32con.OPEN_EXISTING, win32con.FILE_ATTRIBUTE_NORMAL, None) win32file.SetFileTime(winfile, winctime, None, winmtime) winfile.close()
def testFileTimes(self): if issubclass(pywintypes.TimeType, datetime.datetime): from win32timezone import TimeZoneInfo now = datetime.datetime.now(tz=TimeZoneInfo.local()) nowish = now + datetime.timedelta(seconds=1) later = now + datetime.timedelta(seconds=120) else: rc, tzi = win32api.GetTimeZoneInformation() bias = tzi[0] if rc == 2: # daylight-savings is in effect. bias += tzi[-1] bias *= 60 # minutes to seconds... tick = int(time.time()) now = pywintypes.Time(tick + bias) nowish = pywintypes.Time(tick + bias + 1) later = pywintypes.Time(tick + bias + 120) filename = tempfile.mktemp("-testFileTimes") # Windows docs the 'last time' isn't valid until the last write # handle is closed - so create the file, then re-open it to check. open(filename, "w").close() f = win32file.CreateFile( filename, win32file.GENERIC_READ | win32file.GENERIC_WRITE, 0, None, win32con.OPEN_EXISTING, 0, None) try: ct, at, wt = win32file.GetFileTime(f) self.assertTrue( ct >= now, "File was created in the past - now=%s, created=%s" % (now, ct)) self.assertTrue(now <= ct <= nowish, (now, ct)) self.assertTrue( wt >= now, "File was written-to in the past now=%s, written=%s" % (now, wt)) self.assertTrue(now <= wt <= nowish, (now, wt)) # Now set the times. win32file.SetFileTime(f, later, later, later) # Get them back. ct, at, wt = win32file.GetFileTime(f) # XXX - the builtin PyTime type appears to be out by a dst offset. # just ignore that type here... if issubclass(pywintypes.TimeType, datetime.datetime): self.assertEqual(ct, later) self.assertEqual(at, later) self.assertEqual(wt, later) finally: f.Close() os.unlink(filename)
def set_created_time(file, timestamp): # Enormous hack to set the creation time of a file on Windows. # From https://stackoverflow.com/a/4996407/252218 wintime = pywintypes.Time(timestamp) winfile = win32file.CreateFile( str(file.absolute()), win32con.GENERIC_WRITE, win32con.FILE_SHARE_READ | win32con.FILE_SHARE_WRITE | win32con.FILE_SHARE_DELETE, None, win32con.OPEN_EXISTING, win32con.FILE_ATTRIBUTE_NORMAL, None) win32file.SetFileTime(winfile, wintime, None, None) winfile.close()
def setFileDates(fileName): """Sets file modification and creation dates to the specified value""" dates = datetime.utcfromtimestamp(os.path.getmtime(fileName)) if __use_win_32: filehandle = win32file.CreateFile(fileName, win32file.GENERIC_WRITE, 0, None, win32con.OPEN_EXISTING, 0, None) win32file.SetFileTime(filehandle, *(dates, ) * 3) filehandle.close() else: os.utime(fileName, (time.mktime(dates.utctimetuple()), ) * 2)
def set_file_dates(filename, dates): # accepts dates argument as in order: # created; acessed; modified dates = [do_ttime(i) for i in dates] if __use_win_32: filehandle = win32file.CreateFile(filename, win32file.GENERIC_WRITE, 0, None, win32con.OPEN_EXISTING, 0, None) win32file.SetFileTime(filehandle, *[wintime(int(i)) for i in dates]) filehandle.close() else: os.utime(filename, tuple(dates[1:]))
def change_file_creation_time(fname, newtime): wintime = pywintypes.Time(newtime).replace(microsecond=newtime.microsecond) winfile = win32file.CreateFile( fname, win32con.GENERIC_WRITE, win32con.FILE_SHARE_READ | win32con.FILE_SHARE_WRITE | win32con.FILE_SHARE_DELETE, None, win32con.OPEN_EXISTING, win32con.FILE_ATTRIBUTE_NORMAL, None) win32file.SetFileTime(winfile, wintime, None, None) winfile.close()
def changeFileCreationTime(fname, date, time): newtime = datetime.strptime(date + ' ' + time, '%Y-%m-%d %H:%M:%S') wintime = pywintypes.Time(newtime.timestamp()) winfile = win32file.CreateFile( fname, win32con.GENERIC_WRITE, win32con.FILE_SHARE_READ | win32con.FILE_SHARE_WRITE | win32con.FILE_SHARE_DELETE, None, win32con.OPEN_EXISTING, win32con.FILE_ATTRIBUTE_NORMAL, None) win32file.SetFileTime(winfile, wintime, wintime, wintime) winfile.close()
def copy_file_time(source, destination): if os.name == 'nt': src_file = win32file.CreateFile(source, win32file.GENERIC_READ, 0, None, win32file.OPEN_EXISTING, win32con.FILE_ATTRIBUTE_NORMAL, 0) ct, at, wt = win32file.GetFileTime(src_file) src_file.close() dst_file = win32file.CreateFile( destination, win32file.GENERIC_READ | win32con.GENERIC_WRITE, 0, None, win32con.OPEN_EXISTING, win32con.FILE_ATTRIBUTE_NORMAL, None) win32file.SetFileTime(dst_file, ct, at, wt) dst_file.close()
def changeFileCreationTime(fname, newtime): '''Windows下更改文件创建时间 ''' wintime = pywintypes.Time(newtime) winfile = win32file.CreateFile( fname, win32con.GENERIC_WRITE, win32con.FILE_SHARE_READ | win32con.FILE_SHARE_WRITE | win32con.FILE_SHARE_DELETE, None, win32con.OPEN_EXISTING, win32con.FILE_ATTRIBUTE_NORMAL, None) win32file.SetFileTime(winfile, wintime, None, None) winfile.close()
def setTime(self, filename, date_tuple): (day, month, year) = date_tuple generated_time = datetime.datetime(year, month, day) target_time = pywintypes.Time(generated_time) winfile = win32file.CreateFile( filename, win32con.GENERIC_WRITE, win32con.FILE_SHARE_READ | win32con.FILE_SHARE_WRITE | win32con.FILE_SHARE_DELETE, None, win32con.OPEN_EXISTING, win32con.FILE_ATTRIBUTE_NORMAL, None) win32file.SetFileTime(winfile, target_time, None, None) winfile.close()
def testFileTimes(self): from win32timezone import TimeZoneInfo # now() is always returning a timestamp with microseconds but the # file APIs all have zero microseconds, so some comparisons fail. now = datetime.datetime.now(tz=TimeZoneInfo.utc()).replace( microsecond=0) nowish = now + datetime.timedelta(seconds=1) later = now + datetime.timedelta(seconds=120) filename = tempfile.mktemp("-testFileTimes") # Windows docs the 'last time' isn't valid until the last write # handle is closed - so create the file, then re-open it to check. open(filename, "w").close() f = win32file.CreateFile( filename, win32file.GENERIC_READ | win32file.GENERIC_WRITE, 0, None, win32con.OPEN_EXISTING, 0, None, ) try: ct, at, wt = win32file.GetFileTime(f) self.failUnless( ct >= now, "File was created in the past - now=%s, created=%s" % (now, ct), ) self.failUnless(now <= ct <= nowish, (now, ct)) self.failUnless( wt >= now, "File was written-to in the past now=%s, written=%s" % (now, wt), ) self.failUnless(now <= wt <= nowish, (now, wt)) # Now set the times. win32file.SetFileTime(f, later, later, later, UTCTimes=True) # Get them back. ct, at, wt = win32file.GetFileTime(f) # XXX - the builtin PyTime type appears to be out by a dst offset. # just ignore that type here... self.failUnlessEqual(ct, later) self.failUnlessEqual(at, later) self.failUnlessEqual(wt, later) finally: f.Close() os.unlink(filename)
def change_created_time(self, filepath: Path, d_ctime: datetime) -> None: """Change the created time of a given file.""" winfile = win32file.CreateFileW( str(filepath), win32con.GENERIC_WRITE, (win32con.FILE_SHARE_READ | win32con.FILE_SHARE_WRITE | win32con.FILE_SHARE_DELETE), None, win32con.OPEN_EXISTING, win32con.FILE_ATTRIBUTE_NORMAL, None, ) win32file.SetFileTime(winfile, d_ctime)
def change_file_date(self, filepath: Path, mtime: str = None, ctime: str = None) -> None: """ Change the FS modification and creation dates of a file. Since there is no creation time on GNU/Linux, the ctime will not be taken into account if running on this platform. :param filename: The file to modify :param mtime: The modification time :param ctime: The creation time """ filepath = safe_long_path(filepath) log.debug( f"Setting file dates for {filepath!r} (ctime={ctime!r}, mtime={mtime!r})" ) # Set the creation time first as on macOS using touch will change ctime and mtime. # The modification time will be updated just after, if needed. if ctime: d_ctime = datetime.strptime(str(ctime), "%Y-%m-%d %H:%M:%S") if MAC: cmd = [ "touch", "-mt", d_ctime.strftime("%Y%m%d%H%M.%S"), str(filepath) ] subprocess.check_call(cmd) elif WINDOWS: winfile = win32file.CreateFileW( str(filepath), win32con.GENERIC_WRITE, (win32con.FILE_SHARE_READ | win32con.FILE_SHARE_WRITE | win32con.FILE_SHARE_DELETE), None, win32con.OPEN_EXISTING, win32con.FILE_ATTRIBUTE_NORMAL, None, ) win32file.SetFileTime(winfile, d_ctime) if mtime: d_mtime = mktime(strptime(str(mtime), "%Y-%m-%d %H:%M:%S")) os.utime(filepath, (d_mtime, d_mtime))
def _changeTimestamps(source, destination): stats = os.stat(source) os.utime(destination, (stats.st_atime, stats.st_mtime)) if platform.system() == "Windows": handle = win32file.CreateFile( destination, # file path win32file. GENERIC_WRITE, # must opened with GENERIC_WRITE access 0, None, win32file.OPEN_EXISTING, 0, 0) PyTime = pywintypes.Time(stats.st_ctime) win32file.SetFileTime(handle, PyTime)
def setDatetimeFileCMA(filepath, timestamp): #we need to correct summer saving time #dirty workaround, does the job... datestamp = datetime(timestamp.year, 4, 1) # DST starts last Sunday in March dstOn = datestamp - timedelta(days=datestamp.weekday() + 1) datestamp = datetime(timestamp.year, 11, 1) # DST ends last Sunday in October dstOff = datestamp - timedelta(days=datestamp.weekday() + 1) if dstOn <= timestamp.replace(tzinfo=None) < dstOff: #timestamp inside summer saving time #print timedelta(hours=1) pass else: #print timedelta(0) timestamp = timestamp + timedelta(hours=1) handle = win32file.CreateFile(filepath, win32con.GENERIC_WRITE, 0, None, win32con.OPEN_EXISTING, 0, None) win32file.SetFileTime(handle, timestamp, timestamp, timestamp) handle.close()
def change_file_creation_time(path, new_time): win_time = pywintypes.Time(new_time) new_file = win32file.CreateFile( path, win32file.GENERIC_WRITE, 0, None, win32file.OPEN_EXISTING, 0, 0 ) win32file.SetFileTime( new_file, win_time ) new_file.close()
def setFileCreationTime(fname, newtime): """http://stackoverflow.com/a/4996407/6277151""" if os.name != 'nt': # file creation time can only be changed in Windows return import pywintypes, win32file, win32con wintime = pywintypes.Time(newtime) winfile = win32file.CreateFile( fname, win32con.GENERIC_WRITE, win32con.FILE_SHARE_READ | win32con.FILE_SHARE_WRITE | win32con.FILE_SHARE_DELETE, None, win32con.OPEN_EXISTING, win32con.FILE_ATTRIBUTE_NORMAL, None) win32file.SetFileTime(winfile, wintime, None, None) winfile.close()
def change_creation_date(eml_fp, sent_date): """ change file creation date to match sent_date Parameters: ----------- eml_fp: str full path to the file sent_date: str date at which email was sent Returns: -------- """ emailtime = dateutil_parse(sent_date).timetuple() log_msg = "SENT ON: %s" % (str(emailtime)) logging.debug(log_msg) atime = int(mktime(emailtime)) times = (atime, atime) try: #new_fn_fp = abspath(join(dirname,new_filename)) #new filename full path #ChangeFileCreationTime(dirname+"\\"+new_filename,atime) #ChangeFileCreationTime(new_fn_fp,atime) wintime = pywintypes.Time(atime) try: #HERE WE TRY TO CHANGE the FILE CREATE DATE winfile = win32file.CreateFile( eml_fp, win32con.GENERIC_WRITE, win32con.FILE_SHARE_READ | win32con.FILE_SHARE_WRITE | win32con.FILE_SHARE_DELETE, None, win32con.OPEN_EXISTING, win32con.FILE_ATTRIBUTE_NORMAL, None) win32file.SetFileTime(winfile, wintime, None, None) winfile.close() except: log_msg = "179 error changing creation date for fname :%s" % ( eml_fp) logging.info(log_msg) if stop_error: raise #NOW WE CHANGE THE LAST CHANGE TIME #setFileAttributes(eml_fp, from_sender, title,comments) os.utime(eml_fp, times) except: log_msg = "294 failed to change file flags for : %s" % (eml_fp) logging.info(log_msg) raise
def touch(name): if sys.platform == 'win32': import win32file, win32con, pywintypes access = win32file.GENERIC_WRITE share = (win32file.FILE_SHARE_DELETE | win32file.FILE_SHARE_READ | win32file.FILE_SHARE_WRITE) create = win32file.OPEN_EXISTING mtime = time.gmtime() handle = win32file.CreateFile( name, access, share, None, create, win32file.FILE_ATTRIBUTE_NORMAL | win32con.FILE_FLAG_BACKUP_SEMANTICS, None) try: newTime = pywintypes.Time(mtime) win32file.SetFileTime(handle, newTime, newTime, newTime) finally: win32file.CloseHandle(handle) else: os.utime(name, None)