python修改单个文件

修改文件代码

update_bios.py

# -*- coding: utf-8 -*-
import os

p = open('D:\\git\\work_space\\update_file\\BIOS.txt', 'r', encoding='utf-16-le')  # 创建一个读的文本
q = open('D:\\git\\work_space\\update_file\\BIOS2.txt', 'w', encoding='utf-16-le')  # 创建一个写的文本
# 因所修改文件种有DEFAULT = 0x0000重复项,现在只要修改Quiet Boot下面的单个DEFAULT = 0x0001,number>number2来判断是否到'POPUPSELCONTROL ("Quiet Boot")	// Do NOT change this line'下面
number = 0  # number为用于下面判断修改的内容是否在'POPUPSELCONTROL ("Quiet Boot")	// Do NOT change this line'下面
number2 = 0  # number2为'POPUPSELCONTROL ("Quiet Boot")	// Do NOT change this line'所在行数
n = 0  # n做为1个限制器,为1时候可以修改
for i in p:
    print(i)
    number += 1
    # 判断是否到应该修改地方
    if 'POPUPSELCONTROL ("Quiet Boot")	// Do NOT change this line' in i:
        number2 = number
        n = 1
    # 将"DEFAULT = 0x0000"修改成"DEFAULT = 0x0001"
    if "DEFAULT = 0x0000" in i and number > number2 and n == 1:
        i = "    DEFAULT = 0x0001\n"
        n = 0
    q.write(i)

p.close()
q.close()

# 将修改后的文件复刻回原来的BIOS.txt
pw = open('BIOS.txt', 'w', encoding='utf-16-le')
qr = open('BIOS2.txt', 'r', encoding='utf-16-le')
pw.write(qr.read())
# 关闭文件
pw.close()
qr.close()
# 删除被因修改创造的文件
os.remove('BIOS2.txt')

被修改的文件

BIOS.txt(编码为utf-16-le)

POPUPSELCONTROL ("Pxe Only")    // Do NOT change this line
 TEXT = 0x0084    // Please use this ID to find out the relative string.Do NOT change this line
 ACCESSLEVEL = 0
   // ACCESSLEVEL Options :
   // [0:DEFAULT]
   // [1:EXTENDED_USER]
   // [2:USER]
   // [3:SUPERVISOR]
 DEFAULT = 0x0000
 POWERON = 0x0001
   // DEFAULT/POWERON Options :
   // [0x0000 : Enabled]
   // [0x0001 : Disabled]
 HIDDEN = NO
 HELP = 0x0085    // Do NOT change this line
ENDPOPUPSELCONTROL

POPUPSELCONTROL ("Add EFI Shell To Boot Option")   // Do NOT change this line
 TEXT = 0x0088    // Please use this ID to find out the relative string.Do NOT change this line
 ACCESSLEVEL = 0
   // ACCESSLEVEL Options :
   // [0:DEFAULT]
   // [1:EXTENDED_USER]
   // [2:USER]
   // [3:SUPERVISOR]
 DEFAULT = 0x0001
 POWERON = 0x0001
   // DEFAULT/POWERON Options :
   // [0x0000 : Enabled]
   // [0x0001 : Disabled]
 HIDDEN = NO
 HELP = 0x0089    // Do NOT change this line
ENDPOPUPSELCONTROL

POPUPSELCONTROL ("Quiet Boot") // Do NOT change this line
 TEXT = 0x003A    // Please use this ID to find out the relative string.Do NOT change this line
 ACCESSLEVEL = 0
   // ACCESSLEVEL Options :
   // [0:DEFAULT]
   // [1:EXTENDED_USER]
   // [2:USER]
   // [3:SUPERVISOR]
 DEFAULT = 0x0000
 POWERON = 0x0000
   // DEFAULT/POWERON Options :
   // [0x0000 : Disabled]
   // [0x0001 : Enabled]
 HIDDEN = NO
 HELP = 0x003B    // Do NOT change this line
ENDPOPUPSELCONTROL



NUMERICCONTROL ("")    // Do NOT change this line
 TEXT = 0xFFFF    // Please use this ID to find out the relative string.Do NOT change this line
 ACCESSLEVEL = 0
   // ACCESSLEVEL Options :
   // [0:DEFAULT]
   // [1:EXTENDED_USER]
   // [2:USER]
   // [3:SUPERVISOR]
 HIDDEN = NO
 HELP = 0xFFFF    // Do NOT change this line
ENDNUMERICCONTROL


NUMERICCONTROL ("")    // Do NOT change this line
 TEXT = 0xFFFF    // Please use this ID to find out the relative string.Do NOT change this line
 ACCESSLEVEL = 0
   // ACCESSLEVEL Options :
   // [0:DEFAULT]
   // [1:EXTENDED_USER]
   // [2:USER]
   // [3:SUPERVISOR]
 HIDDEN = NO
 HELP = 0xFFFF    // Do NOT change this line
ENDNUMERICCONTROL

猜你喜欢

转载自blog.csdn.net/dreams_dream/article/details/128311423