Python修改GPS SET文件后无变更生效的问题排查求助
Python修改GPS SET文件后无变更生效的问题排查求助
大家好,我现在遇到一个头疼的问题:我有两台同型号的GPS接收器,其中一台陷入启动循环了,打算把正常设备的mgnShell.set文件里和故障设备不同的字节复制过去,让两个文件完全一致,看看能不能修复故障。
我用Python 3.12在VSCode里写了代码,运行起来没报错,但修改后的故障文件完全没变化,实在搞不懂哪里出问题了,想请大家帮忙看看。
我的代码如下:
from itertools import zip_longest count = 0 with open(r"C:\Users\jimal\Desktop\APP2\mgnShell.set", "r+b") as faulty_magellan, open(r"E:\APP\mgnShell.set", "rb") as magellan: faulty_mgln_binary = faulty_magellan.read() faulty_mgln_byte_array = bytearray(faulty_mgln_binary) magellan_binary = magellan.read() magellan_byte_array = bytearray(magellan_binary) for faulty_mgln_byte, magellan_byte in zip_longest(faulty_mgln_byte_array, magellan_byte_array): count += 1 if faulty_mgln_byte != magellan_byte: faulty_magellan.seek(faulty_mgln_byte_array.index(faulty_mgln_byte)) if magellan_byte is None: faulty_magellan.write(bytes([0x00])) else: faulty_magellan.write(bytes([magellan_byte])) print(f"Done! Count was {count}")
我已经排查过的点:
- 我用了
r+b模式打开故障文件,按道理应该支持读写和修改操作 - 故障文件所在的
APP2文件夹显示是只读属性,但我手动创建TXT文件修改内容、甚至手动修改这个SET文件都能保存成功,说明不是文件权限问题 - 重启过VSCode,之前遇到小问题重启就好,但这次没用
我的疑问:
我本来以为代码逻辑是对的:把两个文件都读成字节数组,逐个对比字节,不一样的地方就把正常文件的字节写到故障文件对应位置。但运行后打开故障文件看,完全没变化,是我代码哪里写错了吗?还是有其他我没考虑到的点?
麻烦大家帮我看看,谢谢了!
内容来源于stack exchange
相关产品推荐
相关产品推荐

