mhammond / pywin32

Python for Windows (pywin32) Extensions
5.01k stars 792 forks source link

An error occurred: 'pywintypes.datetime' object has no attribute 'astimezone' #2164

Closed zybsai closed 9 months ago

zybsai commented 9 months ago

Note that issues in this repository are only for bugs or feature requests in the pywin32.

If you need support or help using this package, please follow these instructions - support or help requests will be closed without comment.

For all bugs, please provide the following information.

When I use pyinstaller to package the code into an executable program, I get an error when I run the executable, but not when I run the source file directly, error: "An error occurred: 'pywintypes.datetime' object has no attribute 'astimezone' ", the error raised by "win32file.SetFileTime(...)"

the test source code as bellow:

import win32file,pywintypes

def change_file_ctime(file_path,new_ctime_stamp):
    handle = win32file.CreateFile(file_path, win32file.GENERIC_WRITE,
                                win32file.FILE_SHARE_READ | win32file.FILE_SHARE_WRITE,
                                None, win32file.OPEN_EXISTING,
                                win32file.FILE_ATTRIBUTE_NORMAL, None)
    date_time = pywintypes.Time(new_ctime_stamp) 
    print(str(date_time.astimezone()))
    print(str(type(date_time)))
    print(str(dir(date_time)))
    if hasattr(date_time,"astimezone"):
        win32file.SetFileTime(File=handle, CreationTime=date_time,LastAccessTime=None,LastWriteTime=None,UTCTimes=False) 
    else:
        print("no astimezone")
    handle.close()

if __name__=="__main__":
        a=input("path:")
        b=float(input("time_stamp:"))
        change_file_ctime(a,b)
        input("press any key...")
mhammond commented 9 months ago

Please see the first 2 lines of your issue.