从配置文件中提取值并作为参数传递

2024-10-03 06:32:50 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我的配置文件:

[account1]

username = user1

password = pwd1

[account2]
username = user2

password = pwd2

[account3]
username = user3

password = pwd3

我需要做的是:

读取confile文件,提取用户名和密码,并将它们作为参数传递给另一个脚本。 我想提取这些值并将它们动态传递给第二个脚本。 我不想在两者之间再建一本词典。

这是我的密码:

#!/usr/bin/python
import ConfigParser

conf = ConfigParser.RawConfigParser()

conf.read('credentials.cfg')

for each_account in conf.sections():

           USER = conf.get(each_account, "username")

           PASSWORD = conf.get(each_account, "password")

process(USER, PASSWORD)

输出:它使用user3和pwd3只调用一次进程。为什么其他两个凭据没有通过?你知道吗


Tags: 脚本密码getconf配置文件usernameaccountpassword
1条回答
网友
1楼 · 发布于 2024-10-03 06:32:50

你不想处理每个用户和密码吗?你知道吗

#!/usr/bin/python
import ConfigParser

conf = ConfigParser.RawConfigParser()
conf.read('credentials.cfg')
for each_account in conf.sections():
           USER = conf.get(each_account, "username")
           PASSWORD = conf.get(each_account, "password")
           process(USER, PASSWORD)

相关问题 更多 >