在Python中将文件最后一行与字符串比较

0 投票
2 回答
533 浏览
提问于 2025-04-17 18:16

我是一名Python新手,正在尝试写一个脚本,处理两个Intel hex文件(一个是我的应用代码,另一个是引导加载程序)。我想从第一个文件中去掉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()

如果有其他更简洁或更安全的建议,我也很欢迎。

更新: 我添加了一行代码来打印最后一行;我得到了预期的输出,但比较每次还是失败。以下是当前的完整程序:

appFile = open("C:/LightLock/Master/Project/Debug/Exe/Light Lock.hex")

appLines = appFile.readlines()

appFile = open("MyAppFile.hex").read()

EOF = appLines[len(appLines)-1]

print(appLines[len(appLines)-1])

if not EOF == (":00000001FF"):
    print("No EOF record in last line of file. File may be corrupted.")
else:
    with open("MyAppFile Plus Boot", "a") as appStrip:
        appStrip.writelines([item for item in appLines[:-1]])

    with open("MyAppFile Plus Boot.hex", "r") as appFile:
        appObcode = appFile.read()

    with open("MyBootFile.hex", "r") as bootFile:
        bootObcode = bootFile.read()

    comboData = appObcode + bootObcode

    with open("MyAppFile Plus Boot.hex", "w") as comboFile:
        comboFile.write(comboData)

更新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.")

但还是没有成功。

2 个回答

0

最后我搞明白了:我写了一些测试代码,输出了Python读取的字符串的长度。结果发现长度是12个字符,虽然只显示了11个。所以我知道其中一个“看不见”的字符可能是回车符或者换行符。我试了这两种,结果是换行符(新的一行)。

这是最终的(可以运行,但“没有优化”)代码:

appFile = open("MyAppFile.hex")

appLines = appFile.readlines()

appFile = open("MyAppFile.hex").read()

EOF = appLines[len(appLines)-1]

if EOF != (":00000001FF\n"):
    print("No EOF record in last line of file. File may be corrupted.")
else:
    with open("MyAppFile and Boot.hex", "a") as appStrip:
        appStrip.writelines([item for item in appLines[:-1]])

    with open("MyAppFile and Boot.hex", "r") as appFile:
        appObcode = appFile.read()

    with open("MyBootFile.hex", "r") as bootFile:
        bootObcode = bootFile.read()

    comboData = appObcode + bootObcode

    with open("MyAppFile and Boot.hex", "w") as comboFile:
        comboFile.write(comboData)
0

这是一个更简单的版本:

app = open("app.hex").read()
if not app.endswith(":00000001FF"):
   print("No EOF")
combo = open("combo.hex","w")
combo.write(app)
boot = open("boot.hex").read()
combo.write(boot)
combo.close() # it's automatic after program ended

撰写回答