以下是使用Python编写的示例脚本,它会打开一个配置文件并允许用户修改多个不同的值:
# 打开配置文件
config_file = open('config.txt', 'r')
# 读取配置文件的内容并将其存储为字典
config = {}
for line in config_file:
key, value = line.strip().split('=')
config[key] = value
# 用户输入要修改的值
print('请选择您要修改的值:')
print('1. 端口号')
print('2. IP地址')
option = input()
if option == '1':
new_port = input('请输入新的端口号:')
config['port'] = new_port
elif option == '2':
new_ip = input('请输入新的IP地址:')
config['ip'] = new_ip
else:
print('无效选项')
# 将新的配置写回文件
with open('config.txt', 'w') as file:
for key, value in config.items():
file.write(key + '=' + value + '\n')
print('配置已更新')
在此示例中,我们首先打开名为“config.txt”的文件,并将其内容读取到一个字典中。然后,我们询问用户要修改的值,并根据选项提示用户输入新值。最后,我们将新的配置写回文件,并向用户显示一个成功的消息。这个脚本可以根据需要进行修改,以允许用户修改任意数量的值。