系统相关
首页 > 系统相关> > Windows本地资源交互

Windows本地资源交互

作者:互联网

运行信息

进程管理

文件操作

ini文件解析

import configparser


def doini():
    filename = "test.ini"

    # 加载ini
    iniFile = configparser.ConfigParser()
    iniFile.read(filename)

    # 添加或更新节点
    section, key, value = "request", "timeout", "30"
    if not iniFile.has_section(section):
        iniFile.add_section(section)
    iniFile.set(section, key, value)

    # 删除节点
    section, key, value = "request", "retry", "5"
    if iniFile.has_section(section):
        if iniFile.has_option(section, key):
            iniFile.remove_option(section, key)
    if len(iniFile.options(section)) == 0:
        iniFile.remove_section(section)

    # 回写ini
    iniFile.write(open(filename, "w"))


if __name__ == "__main__":
    doini()

注册表操作

import winreg


def doreg(regList):
    # root
    root, path = winreg.HKEY_CURRENT_USER, r"ttt\www"
    reg = winreg.CreateKey(root, path)

    try:
        # 添加或更新
        name, value = "host", "sss"
        winreg.SetValueEx(reg, name, 0, winreg.REG_SZ, value)

        # 删除
        name = "port"
        for i in range(0, winreg.QueryInfoKey(reg)[1]):
            if (winreg.EnumValue(reg, i)[0] == name):
                winreg.DeleteValue(reg, name)
                break
    except Exception as e:
        raise e
    finally:
        winreg.CloseKey(reg)

标签:winreg,full,Windows,交互,section,iniFile,本地,path,os
来源: https://blog.csdn.net/weixin_37702639/article/details/117464840