import win32security import ntsecuritycon as con def set_owner(file_path, new_owner): # Lookup the user SID sd = win32security.GetFileSecurity(file_path, win32security.OWNER_SECURITY_INFORMATION) sid, _ = win32security.LookupAccountName(None, new_owner) # Set the new owner sd.SetSecurityDescriptorOwner(sid, 0) win32security.SetFileSecurity(file_path, win32security.OWNER_SECURITY_INFORMATION, sd) print(f"Ownership of {file_path} changed to {new_owner}") # Usage: file_path = "C:\\Temp\\example_file.txt" new_owner = "YOUR-PC\\YourUsername" # Replace with your Windows username set_owner(file_path, new_owner)