Last active
December 28, 2015 18:28
-
-
Save ranlix/7542864 to your computer and use it in GitHub Desktop.
工作需要,每个库文件都需要生成单独的cfg文件,并且需要筛选目录下存在的库文件,对比文档里的库文件对应的id,生成内容为“id,4(level)”的cfg文件。
注:这次是在sublime Text2里添加了PEP8的插件,从此开始注重编码规范
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*-coding:utf-8 -*- | |
import os | |
path = r"C:\Users\alex\Desktop\debug" | |
original_file = open(r"module.txt", "r") # read file | |
content = original_file.readlines() | |
def lib_list_without_so(yourdir): | |
""" Create a list which contains all the libs without '.so' | |
Call lib_list_without_so(dir) | |
For an example, if there is some files like "libaaa.so", | |
"libbbb.so" in that path "dir", call this function will return a link, | |
which contains ["aaa","bbb"] | |
""" | |
file_list = os.listdir(yourdir) | |
new_lib_so = [] | |
for i in file_list: | |
new_lib_so.append(i[3:-3]) # add items without .so to the new list | |
return new_lib_so | |
def cfg_creater(): | |
"""Create cfg files if there are ids in the txt file, | |
the cfg name is the lib name. | |
""" | |
libList = lib_list_without_so(path) # Call the function to create lib list | |
for item in content: | |
if item.startswith("0"): # Judge whether current lib has id | |
item = item.strip('\n').split("\t") | |
if item[1] in libList: | |
# Hexadecimal numbers need to remain format: | |
# 0x (lowercase) + number (uppercase) | |
file_cont = (item[0][:2].lower() + item[0][2:].upper()) + ",4" | |
file_name = item[1] + ".cfg" # Define .cfg file's name | |
new_file = open(file_name, "w") # Create .cfg file | |
new_file.write(file_cont) | |
new_file.close() | |
if __name__ == '__main__': | |
cfg_creater() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment