我是一个Python noob,正试图编写一个脚本来获取两个intelhex文件(一个用于我的应用程序代码,一个用于引导加载程序),从第一个文件中剥离EOF记录,将第二个文件附加到第一个文件的剥离版本,并另存为一个新文件。我已经把所有的东西都准备好了,但后来我决定做得更好:我要确保第一个文件的最后一行真正匹配Intel EOF记录格式。不过,我似乎不能正确地理解这个条件的语法。在
appFile = open("MyAppFile.hex", "r")
lines = appFile.readlines()
appFile.close()
appStrip = open("MyAppFile and BootFile.hex",'w')
if appStrip.readline[:] == ":00000001FF": #Python complains about "builtin_function_or_method" not subscriptable here
appStrip.writelines([item for item in lines[:-1]])
appStrip.close()
else:
print("No EOF record in last line. File may be corrupted.")
appFile = open("MyAppFile and BootFile", "r")
appObcode = appFile.read()
appFile.close()
bootFile = open("MyBootFile", "r")
bootObcode = bootFile.read()
bootFile.close()
comboData = appObcode + bootObcode
comboFile = open("MyAppFile and BootFile", "w")
comboFile.write(comboData)
comboFile.close()
任何其他的建议,以一个更清洁或更安全的版本,也欢迎。在
更新: 添加了一行打印最后一行;我得到了预期的输出,但是每次比较仍然失败。以下是当前的完整程序:
^{pr2}$更新2: 尝试修改支票以包含回车符和换行符,如下所示:
EOF = appLines[len(appLines)-1]
print(EOF)
if EOF != (":00000001FF","\r","\n"):
print("No EOF record in last line of file. File may be corrupted.")
还是不走运。在
下面是一个更简单的版本:
最后解决了这个问题:我编写了一些测试代码来输出Python正在读取的字符串的长度。结果是12个字符,但只有11个字符显示。所以我知道其中一个“看不见”的字符一定是回车符或换行符。两者都试过了;结果是换行(换行)。在
这里是“未优化的代码”:
相关问题 更多 >
编程相关推荐