導航:首頁 > 文件管理 > python配置文件中心

python配置文件中心

發布時間:2023-09-03 18:03:30

『壹』 如何使用Python3讀取配置文件

ini文件簡介

ini是我們常見到的配置文件格式之一。

ini是微軟Windows操作系統中的文件擴展名(也常用在其他系統)。

INI是英文「初始化(Initial)」的縮寫。正如該術語所表示的,INI文件被用來對操作系統或特定程序初始化或進行參數設置。

網路

通過它,可以將經常需要改變的參數保存起來(而且還可讀),使程序更加的靈活。

我先給出一個ini文件的示例。
[School]
ip = 10.15.40.123
mask = 255.255.255.0
gateway = 10.15.40.1
dns = 211.82.96.1

[Match]
ip = 172.17.29.120
mask = 255.255.255.0
gateway = 172.17.29.1
dns = 0.0.0.0

這個配置文件中保存的是不同場合下的IP設置參數。

下面將以生成和讀取這個配置文件為例,進行講解。

Python(v3)讀取方法

首先,Python讀取ini配置需要用到ConfigParser包,所以要先載入它。
import configparser

之後我們需要載入配置文件。
config=configparser.ConfigParser()

#IpConfig.ini可以是一個不存在的文件,意味著准備新建配置文件。
config.read("IpConfig.ini")

接下來,我們可以使用configparser.add_section()向配置文件中添加一個Section。
#添加節School
config.add_section("School")

注意:如果文件中已經存在相應的項目,則不能再增加同名的節。

然後可以使用configparser.set()在節School中增加新的參數。
#添加新的IP地址參數
config.set("School","IP","192.168.1.120")
config.set("School","Mask","255.255.255.0")
config.set("School","Gateway","192.168.1.1")
config.set("School","DNS","211.82.96.1")

你可以以同樣的方式增加其它幾項。
#由於ini文件中可能有同名項,所以做了異常處理
try:
config.add_section("Match")
config.set("Match","IP","172.17.29.120")
config.set("Match","Mask","255.255.255.0")
config.set("Match","Gateway","172.17.29.1")
config.set("Match","DNS","0.0.0.0")
except configparser.DuplicateSectionError:
print("Section 'Match' already exists")

增加完所有需要的項目後,要記得使用configparser.write()進行寫入操作。
config.write(open("IpConfig.ini", "w"))

以上就是寫入配置文件的過程。

接下來我們使用configparser.get()讀取剛才寫入配置文件中的參數。讀取之前要記得讀取ini文件。
ip=config.get("School","IP")
mask=config.get("School","mask")
gateway=config.get("School","Gateway")
dns=config.get("School","DNS")

print((ip,mask+"\n"+gateway,dns))

完整示例

下面是一個完整的示常式序,他將生成一個IpConfig.ini的配置文件,再讀取文件中的數據,輸出到屏幕上。
# -*- coding: utf-8 -*-

import configparser

#讀取配置文件
config=configparser.ConfigParser()
config.read("IpConfig.ini")

#寫入宿舍配置文件
try:
config.add_section("School")
config.set("School","IP","10.15.40.123")
config.set("School","Mask","255.255.255.0")
config.set("School","Gateway","10.15.40.1")
config.set("School","DNS","211.82.96.1")
except configparser.DuplicateSectionError:
print("Section 'School' already exists")

#寫入比賽配置文件
try:
config.add_section("Match")
config.set("Match","IP","172.17.29.120")
config.set("Match","Mask","255.255.255.0")
config.set("Match","Gateway","172.17.29.1")
config.set("Match","DNS","0.0.0.0")
except configparser.DuplicateSectionError:
print("Section 'Match' already exists")

#寫入配置文件
config.write(open("IpConfig.ini", "w"))

ip=config.get("School","IP")
mask=config.get("School","mask")
gateway=config.get("School","Gateway")
dns=config.get("School","DNS")

print((ip,mask+"\n"+gateway,dns))

『貳』 【Python基礎】PIP 鏡像源配置輕松搞定

pip 是 python 必不可少的的包管理工具,但是要在國內用得爽,必須要配置鏡像源。

有哪些鏡像站可用,以及如何配置,網上都有很多分享了。

我常用的是  阿里雲鏡像站 。

這里有一點比較麻煩的地方,就是是 linux 和 Windows 環境下的 pip 配置文件的名字和位置都不同,經常混淆。

今天就教大家一招,快速搞定:

執行完上面兩條命令就可以啦。

pip config set 命令能自動把配置寫入到用戶對應的配置文件中:

[global]

index-url = https://mirrors.aliyun.com/pypi/simple/

[install]

trusted-host=mirrors.aliyun.com

命令雖然方便,但是參數格式復雜,記住不太容易,要是想改一下也挺麻煩,

所以我們進一步了解一下細節。

「 下面我以 Windows 系統為例,實際在 Linux 系統也是類似

查看配置

執行 pip config list 命令可以顯示已經有了哪些配置:

pip config list

global.index-url='http://mirrors.aliyun.com/pypi/simple/'

install.trusted-host='mirrors.aliyun.com'

帶上一個 -v 選項:

pip config list -v

For variant 'global', will try loading 'C:\ProgramData\pip\pip.ini'

For variant 'user', will try loading 'C:\Users\davy\pip\pip.ini'

For variant 'user', will try loading 'C:\Users\davy\AppData\Roaming\pip\pip.ini'

For variant 'site', will try loading 'c:\users\davy\appdata\local\programs\python\python38\pip.ini'

global.index-url='http://mirrors.aliyun.com/pypi/simple/'

install.trusted-host='mirrors.aliyun.com'

「 這里有一點不太好的地方是不顯示配置是在哪個文件里。

就把它嘗試獲取的配置文件名完整路徑列出來了。前面的 global 和 user 和 site 分別表示配置文件生效的范圍:

global - 全局,一般不用

user - 當前用戶,推薦

site - 只針對某一個 python 解釋器

可以看到 user 有兩個地方,其中配置任何一個都是可以的。

有的網路文章推薦的手動創建文件地址是前面那個,但是 pip 默認創建的是後者。

編輯配置

在命令行直接執行 pip config edit 會自動為我們打開配置文件,但是在 Windows 環境下還不行:

pip config edit

ERROR: Could not determine editor to use.

需要手動指定一個編輯器,就用記事本就行了:

pip config edit  --editor notepad

「 Linux 系統中編輯器可以使用 vi,也可以是你習慣的其它編輯器

如果你從來沒有設置過,它會報 找不到指定路徑,這是因為相應的文件夾沒有創建。

設置配置

通過 pip config set 命令可以直接設置配置項,它會自動創建沒有的文件夾和文件。但是必須要給定一個配置項:

pip config set

ERROR: Got unexpected number of arguments, expected 2. (example: "pip config set [name] [value]")

我們隨便寫一個配置:

pip config set x.y z

Writing to C:\Users\davy\AppData\Roaming\pip\pip.ini

然後再執行上面的

pip config edit  --editor notepad

就能自動打開配置文件,把拷貝好的配置文件內容貼進去就可以啦。

『叄』 python 怎麼讀取配置文件

python 讀取配置文件方法

#coding=utf8

import ConfigParser

config = ConfigParser.ConfigParser()
config.readfp(open(raw_input("input file name:"), "rb"))
print config.get("global", "ip")


config.ini
[global]
ip = 192.168.1.100 ;ip地址
port = 3306

『肆』 linux新安裝python怎麼配置文件

首先把
Boost庫

頭文件
存放到/usr/include/boost/路徑下,再把
Lib文件
存放到/usr/local/lib/boost/路徑下。修改/etc/profile文件,在此文件中增加如下2個
環境變數

BOOST_INCLUDE=/usr/include/boost
export
BOOST_INCLUDE
BOOST_LIB=/usr/loca...

『伍』 python 如何修改配置文件(ini)的section名稱

deffunc():
input=open(r"c: est.ini")
lines=input.readlines()
input.close()

output=open(r"c: ewest.ini",'w');
forlineinlines:
ifnotline:
break
if'name'inline:
temp=line.split("name")
temp1=temp[0]+'myname'+temp[1]
output.write(temp1)
else:
output.write(line)
output.close()
if__name__=='__main__':
func()

『陸』 Python 快速檢測配置文件是否變更

分享背景:

當項目非常多時隨之而來的配置文件也會變得非常多,而且越發的復雜,有時候上線後才知道線上環境的配置文件不對,那麼我們如何提前來檢測到配置文件有改動了,本文將給你提供一個可以檢測的手段。代碼如下所示


1.導入包並指定目錄


2.初始化配置文件的md5值並入庫


3.檢測新配置文件的md5值是否變化


4.文件進行md5加密處理


5.遍歷指定目錄下文件


6.代碼運行入口

總結:

我們首先要確定我們要檢測的配置文件,然後將它的當前的md5值進行初始化到資料庫,當下次發布前我們可以針對性的進行一次檢測,發現有變更就會提示出來,這樣就可以做到提前知曉變更的配置文件,再人工介入進行重點檢查。

『柒』 python 如何實現配置文件中的配置項加密

可以在寫入配置文件的時候,進行加密,讀取配置後解密即可

比如使用base64加密:
base64.b64encode加密,base64.b64decode解密

『捌』 如何動態修改python logging配置文件

配置文件:

#Configuration for log output
#Naiveloafer
#2012-06-04

[loggers]
keys=root,xzs

[handlers]
keys=consoleHandler,fileHandler,rotatingFileHandler

[formatters]
keys=simpleFmt

[logger_root]
level=DEBUG
#handlers=consoleHandler
#handlers=fileHandler
handlers=rotatingFileHandler

[logger_xzs]
level=DEBUG
handlers=rotatingFileHandler
qualname=xzs
propagate=0

[handler_consoleHandler]
class=StreamHandler
level=DEBUG
formatter=simpleFmt
args=(sys.stdout,)

[handler_fileHandler]
class=FileHandler
level=DEBUG
formatter=simpleFmt
args=("../log/p2pplayer.log", "a")

[handler_rotatingFileHandler]
class=handlers.RotatingFileHandler
level=DEBUG
formatter=simpleFmt
args=("../log/p2pplayer.log", "a", 20*1024*1024, 10)

[formatter_simpleFmt]
format=%(asctime)s - %(name)s - %(levelname)s - %(message)s - [%(filename)s:%(lineno)s]
datefmt=

測試代碼:

def log_test02():
import logging
import logging.config
CONF_LOG = "../conf/p2pplayer_logging.conf"
logging.config.fileConfig(CONF_LOG); # 採用配置文件
logger = logging.getLogger("xzs")
logger.debug("Hello xzs")

logger = logging.getLogger()
logger.info("Hello root")

if __name__ == "__main__":
log_test02()

輸出:

2012-06-04 15:28:05,751 - xzs - DEBUG - Hello xzs - [xlog.py:29]
2012-06-04 15:28:05,751 - root - INFO - Hello root - [xlog.py:32]

具體就不詳細說明了,總之是能夠運行的,這個文件配置搞了我兩天時間。

特別是class=XXXX要注意!!!

閱讀全文

與python配置文件中心相關的資料

熱點內容
word中嵌入本地excel文件 瀏覽:735
w7電腦如何連接網路列印機共享的列印機 瀏覽:474
文件標題是什麼 瀏覽:575
js獲得粘貼的數據 瀏覽:384
什麼是pdb文件 瀏覽:89
windowsphone微信 瀏覽:128
小米3基帶文件 瀏覽:763
新建文件路徑是什麼 瀏覽:504
word默認擴展名 瀏覽:753
網路電話插口如何接線的 瀏覽:330
美妝相機2016版本 瀏覽:901
五年級學什麼編程最好 瀏覽:471
沈陽什麼app騎手好 瀏覽:596
微信群成員頭像是灰色 瀏覽:495
找房用什麼app 瀏覽:174
osx賬戶恢復配置文件 瀏覽:966
安卓m1卡破解 瀏覽:557
系統保護文件是什麼 瀏覽:200
冒險島老物品代碼 瀏覽:834
南航app如何綁定護照 瀏覽:140

友情鏈接