如何在變量中包含外部配置文件並在python腳本中使用它們?Python 3包含帶變量的自定義文件
Python的變量文件:(auth.txt)
Username = 'username_here'
Password = 'password_here'
的Python腳本(test.py)
print('Username:', Username)
print('Password:', Password)
如何在變量中包含外部配置文件並在python腳本中使用它們?Python 3包含帶變量的自定義文件
Python的變量文件:(auth.txt)
Username = 'username_here'
Password = 'password_here'
的Python腳本(test.py)
print('Username:', Username)
print('Password:', Password)
我用了它;
auth.py
# Python Variables
USR = 'user_here'
PWD = 'pass_here'
test.py
#!/bin/python -B
from auth import *
print('Username:', USR)
print('Password:', PWD)
configparser會讓你把它像一本字典:
的config.txt
[DEFAULT]
USR = user_here
PWD = pass_here
權威性。 py:
個import configparser
config = configparser.ConfigParser()
config.read('./config.txt')
print(config['DEFAULT']['USR'])
print(config['DEFAULT']['PWD'])
收率:
user_here
pass_here
重命名'auth.txt'到'auth.py'和'import'它。 – Blender
其實我把它重命名爲auth.py,然後使用auth import *還有其他方法可以做到這一點嗎? – Tux