python脚本自动生成link文件内容--Apple的学习笔记
2023-07-06 本文已影响0人
applecai
一,前言
iar设置函数跑在ram中加速运行--Apple的学习笔记里面最后提及了批量文件添加到指定段, 若批量文件的文件名字有规律,则可以用星号来替代。但是名字无规律,就要手工一个个加了,就比较麻烦,既然是有规律的字符串,那么当然要交给python去自动生成内容了,最近我几乎无创作发明的灵感,那么也算是近期的一个小小的发明创造物。
二,分分钟完成小工具
#!/usr/bin/python
# -*- coding:utf-8 -*-
import os
filePath = '.\src'
outputfileName = 'output.txt'
Infolist = ['readonly code object ','readonly code section *_init object ']
flist = []
def get_files():
filelist = []
for filepath,dirnames,filenames in os.walk(filePath):
for filename in filenames:
if (filename[-2:] == '.c'):
filelist.append(filename[:-2]+'.o')
return filelist
if __name__ == "__main__":
flist = get_files()
with open(outputfileName, 'w') as file:
for txt in Infolist:
for i in range(len(flist)):
if i%2 == 0:
type = '\n,'
else:
type = ','
file.write(type+txt+flist[i])
file.write("\n------")
生成效果
,readonly code object misc.o,readonly code object stm32f4xx_gpio.o
,readonly code object stm32f4xx_rcc.o,readonly code object stm32f4xx_syscfg.o
,readonly code object stm32f4xx_tim.o,readonly code object stm32f4xx_usart.o
,readonly code object led.o,readonly code object timer.o
------
,readonly code section *_init object misc.o,readonly code section *_init object stm32f4xx_gpio.o
,readonly code section *_init object stm32f4xx_rcc.o,readonly code section *_init object stm32f4xx_syscfg.o
,readonly code section *_init object stm32f4xx_tim.o,readonly code section *_init object stm32f4xx_usart.o
,readonly code section *_init object led.o,readonly code section *_init object timer.o
------
三,小结
所有苦力活,只要找到规律,能总结出步骤的,我都会交给python去处理,这也就是我喜欢创作小工具的主要原因,它能帮我提高工作效率,免费为我打工。古人发明工具是生存、进化的需要,而现代人发明工具是因为懒,哈哈~