import datetime,time start_tm=datetime.datetime.now() time.sleep(2) end_tm=datetime.datetime.now() print((end_tm-start_tm).seconds)
输出
2