from pathlib import Path import os path=Path(r'C:\wechat_rpa\wecom_native_sender.py') text=path.read_text(encoding='utf-8') old=""" after=_file_identity(os.fstat(handle.fileno())) if before!=after or before!=_path_file_identity(path): raise NativeUnavailable('企业微信文件在校验期间发生变化,请重新检测') """ new=""" after=_file_identity(os.fstat(handle.fileno())) if before!=after or before!=_path_file_identity(path): raise NativeUnavailable('企业微信文件在校验期间发生变化,请重新检测') """ assert old in text text=text.replace(old,new,1) compile(text,str(path),'exec') temp=path.with_suffix('.identity-fix-tmp');temp.write_text(text,encoding='utf-8');os.replace(temp,path)