Làm cách nào để tạo tệp .conf bằng Python?

Trong hướng dẫn này, chúng ta sẽ xem xét các tệp cấu hình là gì và với sự trợ giúp của mô-đun ConfigParser, chúng ta sẽ tạo một tệp cấu hình, sửa đổi dữ liệu trong tệp cấu hình, thêm dữ liệu mới vào tệp đó và xóa dữ liệu hiện có khỏi tệp cấu hình. Vì vậy, không chậm trễ nữa, hãy bắt đầu

Tệp cấu hình trong Python là gì?

Các tệp cấu hình thường được gọi là tệp cấu hình là các tệp đặc biệt lưu trữ một số dữ liệu và cài đặt cụ thể cho các chương trình máy tính. Hầu hết các chương trình máy tính đều đọc các tệp cấu hình của chúng khi khởi động và kiểm tra định kỳ các thay đổi trong các tệp cấu hình này

Người dùng có thể sử dụng các tệp này để thay đổi cài đặt của ứng dụng mà không cần biên dịch lại chương trình. Nói chung, mỗi tệp cấu hình bao gồm các phần khác nhau. Mỗi phần chứa các cặp khóa và giá trị giống như từ điển Python

Dưới đây là một tệp cấu hình mẫu bao gồm ba phần là Địa chỉ, Giáo dục và Sở thích của một người.  

[Address]
Name = Aditya Raj
Village = Bhojpur
District = Samastipur
State = Bihar

[Education]
College=IIITA
Branch= IT

[Favorites]
Sport = VolleyBall
Book = Historical Books

Bây giờ chúng ta sẽ tạo file cấu hình trên bằng module ConfigParser trong python

Làm cách nào để tạo tệp cấu hình bằng mô-đun Python ConfigParser?

Để tạo file cấu hình trong python, chúng ta sẽ sử dụng module configparser. Trong lần triển khai sau, chúng tôi tạo một đối tượng ConfigParser và thêm các phần vào đó, về cơ bản là các từ điển chứa các cặp khóa-giá trị. Sau đó, chúng tôi lưu tệp cấu hình với. phần mở rộng ini

#import module
import configparser

#create configparser object
config_file = configparser.ConfigParser[]

#define sections and their key and value pairs
config_file["Address"]={
        "Name": "Aditya Raj",
        "Village": "Bhojpur",
        "District": "Samastipur",
        "State": "Bihar"
        }
config_file["Education"]={
        "College":"IIITA",
        "Branch" : "IT"
        }
config_file["Favorites"]={
        "Sports": "VolleyBall",
        "Books": "Historical Books"
        }

#SAVE CONFIG FILE
with open["person.ini","w"] as file_object:
    config_file.write[file_object]
print["Config file 'person.ini' created"]

#print file content
read_file=open["person.ini","r"]
content=read_file.read[]
print["content of the config file is:"]
print[content]

Đầu ra cho đoạn mã trên là

Config file 'person.ini' created
content of the config file is:
[Address]
name = Aditya Raj
village = Bhojpur
district = Samastipur
state = Bihar

[Education]
college = IIITA
branch = IT

[Favorites]
sports = VolleyBall
books = Historical Books

Làm cách nào để thêm phần mới trong tệp cấu hình được tạo bằng ConfigParser?

Để thêm phần mới vào tệp cấu hình, chúng ta chỉ cần đọc tệp cấu hình trong đối tượng cấu hình, thêm phần mới bằng cách xác định phần ở định dạng từ điển và sau đó chúng ta có thể lưu đối tượng cấu hình vào cùng một tệp

Ở đây trong ví dụ dưới đây, chúng tôi sẽ thêm một phần mới "Vóc dáng" trong người. ini đã chứa các phần Địa chỉ, Giáo dục và Yêu thích

import configparser

#print initial file content
read_file=open["person.ini","r"]
content=read_file.read[]
print["content of the config file is:"]
print[content]

#create new config object
config_object= configparser.ConfigParser[]

#read config file into object
config_object.read["person.ini"]

#Add new section named Physique
config_object["Physique"]={
        "Height": "183 CM",
        "Weight": "70 Kg"
        }

#save the config object back to file
with open["person.ini","w"] as file_object:
    config_object.write[file_object]

#print the new config file
print["Config file 'person.ini' updated"]
print["Updated file content is:"]
nread_file=open["person.ini","r"]
ncontent=nread_file.read[]
print[ncontent]

Đầu ra cho đoạn mã trên là

content of the config file is:
[Address]
name = Aditya Raj
village = Bhojpur
district = Samastipur
state = Bihar

[Education]
college = IIITA
branch = IT

[Favorites]
sports = VolleyBall
books = Historical Books


Config file 'person.ini' updated
Updated file content is:
[Address]
name = Aditya Raj
village = Bhojpur
district = Samastipur
state = Bihar

[Education]
college = IIITA
branch = IT

[Favorites]
sports = VolleyBall
books = Historical Books

[Physique]
height = 183 CM
weight = 70 Kg

Chúng ta cũng có thể sử dụng phương thức

#import module
import configparser

#create configparser object
config_file = configparser.ConfigParser[]

#define sections and their key and value pairs
config_file["Address"]={
        "Name": "Aditya Raj",
        "Village": "Bhojpur",
        "District": "Samastipur",
        "State": "Bihar"
        }
config_file["Education"]={
        "College":"IIITA",
        "Branch" : "IT"
        }
config_file["Favorites"]={
        "Sports": "VolleyBall",
        "Books": "Historical Books"
        }

#SAVE CONFIG FILE
with open["person.ini","w"] as file_object:
    config_file.write[file_object]
print["Config file 'person.ini' created"]

#print file content
read_file=open["person.ini","r"]
content=read_file.read[]
print["content of the config file is:"]
print[content]
1 để thêm một phần mới và sau đó sử dụng phương thức
#import module
import configparser

#create configparser object
config_file = configparser.ConfigParser[]

#define sections and their key and value pairs
config_file["Address"]={
        "Name": "Aditya Raj",
        "Village": "Bhojpur",
        "District": "Samastipur",
        "State": "Bihar"
        }
config_file["Education"]={
        "College":"IIITA",
        "Branch" : "IT"
        }
config_file["Favorites"]={
        "Sports": "VolleyBall",
        "Books": "Historical Books"
        }

#SAVE CONFIG FILE
with open["person.ini","w"] as file_object:
    config_file.write[file_object]
print["Config file 'person.ini' created"]

#print file content
read_file=open["person.ini","r"]
content=read_file.read[]
print["content of the config file is:"]
print[content]
2 để thêm các trường mới trong phần

import configparser

#print initial file content
read_file=open["person.ini","r"]
content=read_file.read[]
print["content of the config file is:"]
print[content]

#create new config object
config_object= configparser.ConfigParser[]

#read config file into object
config_object.read["person.ini"]

#Add new section named Physique
config_object.add_section['Physique']
config_object.set['Physique', 'Height', '183 CM']
config_object.set['Physique', 'Weight', '70 Kg']

#save the config object back to file
with open["person.ini","w"] as file_object:
    config_object.write[file_object]

#print the updated config file
print["Config file 'person.ini' updated"]
print["Updated file content is:"]
nread_file=open["person.ini","r"]
ncontent=nread_file.read[]
print[ncontent]

đầu ra

content of the config file is:
[Address]
name = Aditya Raj
village = Bhojpur
district = Samastipur
state = Bihar

[Education]
college = IIITA
branch = IT

[Favorites]
sports = VolleyBall
books = Historical Books


Config file 'person.ini' updated
Updated file content is:
[Address]
name = Aditya Raj
village = Bhojpur
district = Samastipur
state = Bihar

[Education]
college = IIITA
branch = IT

[Favorites]
sports = VolleyBall
books = Historical Books

[Physique]
height = 183 CM
weight = 70 Kg

Trong ví dụ trên, chúng ta có thể thấy rằng phương thức

#import module
import configparser

#create configparser object
config_file = configparser.ConfigParser[]

#define sections and their key and value pairs
config_file["Address"]={
        "Name": "Aditya Raj",
        "Village": "Bhojpur",
        "District": "Samastipur",
        "State": "Bihar"
        }
config_file["Education"]={
        "College":"IIITA",
        "Branch" : "IT"
        }
config_file["Favorites"]={
        "Sports": "VolleyBall",
        "Books": "Historical Books"
        }

#SAVE CONFIG FILE
with open["person.ini","w"] as file_object:
    config_file.write[file_object]
print["Config file 'person.ini' created"]

#print file content
read_file=open["person.ini","r"]
content=read_file.read[]
print["content of the config file is:"]
print[content]
1 lấy tên phần làm đối số trong khi phương thức
#import module
import configparser

#create configparser object
config_file = configparser.ConfigParser[]

#define sections and their key and value pairs
config_file["Address"]={
        "Name": "Aditya Raj",
        "Village": "Bhojpur",
        "District": "Samastipur",
        "State": "Bihar"
        }
config_file["Education"]={
        "College":"IIITA",
        "Branch" : "IT"
        }
config_file["Favorites"]={
        "Sports": "VolleyBall",
        "Books": "Historical Books"
        }

#SAVE CONFIG FILE
with open["person.ini","w"] as file_object:
    config_file.write[file_object]
print["Config file 'person.ini' created"]

#print file content
read_file=open["person.ini","r"]
content=read_file.read[]
print["content of the config file is:"]
print[content]
2 lấy tên phần làm đối số đầu tiên, tên trường làm đối số thứ hai và giá trị cho trường làm đối số thứ ba

Hai phương pháp này cũng có thể được sử dụng trong khi tạo tệp cấu hình mới để thêm các phần và trường vào tệp thay vì sử dụng từ điển như chúng tôi đã thực hiện trong ví dụ này

Làm cách nào để cập nhật dữ liệu trong tệp cấu hình?

Vì chúng tôi đã xác định các phần của tệp cấu hình là từ điển, nên các thao tác áp dụng trên từ điển cũng được áp dụng trên các phần của tệp cấu hình. Chúng tôi có thể thêm các trường trong bất kỳ phần nào của tệp cấu hình hoặc sửa đổi giá trị của trường theo cách tương tự như cách chúng tôi thực hiện với các mục từ điển

Trong đoạn mã sau, chúng tôi đã thêm một trường mới “Năm” trong phần “Giáo dục” của người. ini và sửa đổi giá trị của trường “Chi nhánh” trong tệp

import configparser

#print initial file content
read_file=open["person.ini","r"]
content=read_file.read[]
print["content of the config file is:"]
print[content]

#create new config object
config_object= configparser.ConfigParser[]

#read config file into object
config_object.read["person.ini"]

#update value of a field in a section
config_object["Education"]["Branch"]="MBA"

#add a new field in a section
config_object["Education"].update[{"Year":"Final"}]

#save the config object back to file
with open["person.ini","w"] as file_object:
    config_object.write[file_object]

#print updated content
print["Config file 'person.ini' updated"]
print["Updated file content is:"]
nread_file=open["person.ini","r"]
ncontent=nread_file.read[]
print[ncontent]

đầu ra

content of the config file is:
[Address]
name = Aditya Raj
village = Bhojpur
district = Samastipur
state = Bihar

[Education]
college = IIITA
branch = IT

[Favorites]
sports = VolleyBall
books = Historical Books

[Physique]
height = 183 CM
weight = 70 Kg


Config file 'person.ini' updated
Updated file content is:
[Address]
name = Aditya Raj
village = Bhojpur
district = Samastipur
state = Bihar

[Education]
college = IIITA
branch = MBA
year = Final

[Favorites]
sports = VolleyBall
books = Historical Books

[Physique]
height = 183 CM
weight = 70 Kg

Trong ví dụ trên, chúng ta có thể sử dụng phương thức

#import module
import configparser

#create configparser object
config_file = configparser.ConfigParser[]

#define sections and their key and value pairs
config_file["Address"]={
        "Name": "Aditya Raj",
        "Village": "Bhojpur",
        "District": "Samastipur",
        "State": "Bihar"
        }
config_file["Education"]={
        "College":"IIITA",
        "Branch" : "IT"
        }
config_file["Favorites"]={
        "Sports": "VolleyBall",
        "Books": "Historical Books"
        }

#SAVE CONFIG FILE
with open["person.ini","w"] as file_object:
    config_file.write[file_object]
print["Config file 'person.ini' created"]

#print file content
read_file=open["person.ini","r"]
content=read_file.read[]
print["content of the config file is:"]
print[content]
5 để thêm các trường mới cũng như sửa đổi các trường hiện có. Nếu trường được cung cấp làm đối số tồn tại trong tệp, nó sẽ cập nhật trường nếu không thì một trường mới được tạo

Làm cách nào để xóa dữ liệu khỏi tệp cấu hình?

Chúng tôi có thể xóa dữ liệu khỏi các tệp cấu hình bằng cách sử dụng mô-đun

#import module
import configparser

#create configparser object
config_file = configparser.ConfigParser[]

#define sections and their key and value pairs
config_file["Address"]={
        "Name": "Aditya Raj",
        "Village": "Bhojpur",
        "District": "Samastipur",
        "State": "Bihar"
        }
config_file["Education"]={
        "College":"IIITA",
        "Branch" : "IT"
        }
config_file["Favorites"]={
        "Sports": "VolleyBall",
        "Books": "Historical Books"
        }

#SAVE CONFIG FILE
with open["person.ini","w"] as file_object:
    config_file.write[file_object]
print["Config file 'person.ini' created"]

#print file content
read_file=open["person.ini","r"]
content=read_file.read[]
print["content of the config file is:"]
print[content]
6 và
#import module
import configparser

#create configparser object
config_file = configparser.ConfigParser[]

#define sections and their key and value pairs
config_file["Address"]={
        "Name": "Aditya Raj",
        "Village": "Bhojpur",
        "District": "Samastipur",
        "State": "Bihar"
        }
config_file["Education"]={
        "College":"IIITA",
        "Branch" : "IT"
        }
config_file["Favorites"]={
        "Sports": "VolleyBall",
        "Books": "Historical Books"
        }

#SAVE CONFIG FILE
with open["person.ini","w"] as file_object:
    config_file.write[file_object]
print["Config file 'person.ini' created"]

#print file content
read_file=open["person.ini","r"]
content=read_file.read[]
print["content of the config file is:"]
print[content]
7 trong mô-đun configparser.
#import module
import configparser

#create configparser object
config_file = configparser.ConfigParser[]

#define sections and their key and value pairs
config_file["Address"]={
        "Name": "Aditya Raj",
        "Village": "Bhojpur",
        "District": "Samastipur",
        "State": "Bihar"
        }
config_file["Education"]={
        "College":"IIITA",
        "Branch" : "IT"
        }
config_file["Favorites"]={
        "Sports": "VolleyBall",
        "Books": "Historical Books"
        }

#SAVE CONFIG FILE
with open["person.ini","w"] as file_object:
    config_file.write[file_object]
print["Config file 'person.ini' created"]

#print file content
read_file=open["person.ini","r"]
content=read_file.read[]
print["content of the config file is:"]
print[content]
6 được sử dụng để xóa một trường khỏi bất kỳ phần nào và
#import module
import configparser

#create configparser object
config_file = configparser.ConfigParser[]

#define sections and their key and value pairs
config_file["Address"]={
        "Name": "Aditya Raj",
        "Village": "Bhojpur",
        "District": "Samastipur",
        "State": "Bihar"
        }
config_file["Education"]={
        "College":"IIITA",
        "Branch" : "IT"
        }
config_file["Favorites"]={
        "Sports": "VolleyBall",
        "Books": "Historical Books"
        }

#SAVE CONFIG FILE
with open["person.ini","w"] as file_object:
    config_file.write[file_object]
print["Config file 'person.ini' created"]

#print file content
read_file=open["person.ini","r"]
content=read_file.read[]
print["content of the config file is:"]
print[content]
7 được sử dụng để xóa toàn bộ phần của tệp cấu hình

import configparser

#print initial file content
read_file=open["person.ini","r"]
content=read_file.read[]
print["content of the config file is:"]
print[content]

#create new config object
config_object= configparser.ConfigParser[]

#read config file into object
config_object.read["person.ini"]

#delete a field in a section
config_object.remove_option['Education', 'Year']

#delete a section
config_object.remove_section['Physique']

#save the config object back to file
with open["person.ini","w"] as file_object:
    config_object.write[file_object]

#print new config file
print["Config file 'person.ini' updated"]
print["Updated file content is:"]
nread_file=open["person.ini","r"]
ncontent=nread_file.read[]
print[ncontent]

đầu ra

#import module
import configparser

#create configparser object
config_file = configparser.ConfigParser[]

#define sections and their key and value pairs
config_file["Address"]={
        "Name": "Aditya Raj",
        "Village": "Bhojpur",
        "District": "Samastipur",
        "State": "Bihar"
        }
config_file["Education"]={
        "College":"IIITA",
        "Branch" : "IT"
        }
config_file["Favorites"]={
        "Sports": "VolleyBall",
        "Books": "Historical Books"
        }

#SAVE CONFIG FILE
with open["person.ini","w"] as file_object:
    config_file.write[file_object]
print["Config file 'person.ini' created"]

#print file content
read_file=open["person.ini","r"]
content=read_file.read[]
print["content of the config file is:"]
print[content]
0

Trong ví dụ trên, chúng ta có thể thấy rằng phương thức

#import module
import configparser

#create configparser object
config_file = configparser.ConfigParser[]

#define sections and their key and value pairs
config_file["Address"]={
        "Name": "Aditya Raj",
        "Village": "Bhojpur",
        "District": "Samastipur",
        "State": "Bihar"
        }
config_file["Education"]={
        "College":"IIITA",
        "Branch" : "IT"
        }
config_file["Favorites"]={
        "Sports": "VolleyBall",
        "Books": "Historical Books"
        }

#SAVE CONFIG FILE
with open["person.ini","w"] as file_object:
    config_file.write[file_object]
print["Config file 'person.ini' created"]

#print file content
read_file=open["person.ini","r"]
content=read_file.read[]
print["content of the config file is:"]
print[content]
6 lấy tên phần làm đối số đầu tiên và tên trường làm đối số thứ hai trong khi phương thức
#import module
import configparser

#create configparser object
config_file = configparser.ConfigParser[]

#define sections and their key and value pairs
config_file["Address"]={
        "Name": "Aditya Raj",
        "Village": "Bhojpur",
        "District": "Samastipur",
        "State": "Bihar"
        }
config_file["Education"]={
        "College":"IIITA",
        "Branch" : "IT"
        }
config_file["Favorites"]={
        "Sports": "VolleyBall",
        "Books": "Historical Books"
        }

#SAVE CONFIG FILE
with open["person.ini","w"] as file_object:
    config_file.write[file_object]
print["Config file 'person.ini' created"]

#print file content
read_file=open["person.ini","r"]
content=read_file.read[]
print["content of the config file is:"]
print[content]
7 lấy tên của phần cần xóa làm đối số của nó

Phần kết luận

Trong hướng dẫn này, chúng ta đã biết các tệp cấu hình là gì và chúng ta cũng đã thấy cách tạo và thao tác các tệp cấu hình với sự trợ giúp của mô-đun trình cấu hình Python. học tập vui vẻ. 🙂

Là gì. conf bằng Python?

Tệp cấu hình được sử dụng để lưu trữ các cặp giá trị khóa hoặc một số thông tin có thể định cấu hình có thể được đọc hoặc truy cập trong mã và tại một số thời điểm.

Có gì trong một. tập tin conf?

Tệp cấu hình, thường được rút ngắn thành tệp cấu hình, xác định các tham số, tùy chọn, cài đặt và tùy chọn áp dụng cho hệ điều hành [OS], thiết bị cơ sở hạ tầng và ứng dụng trong ngữ cảnh CNTT< . .

Làm cách nào để tạo tệp cấu hình JSON trong Python?

Sử dụng tệp cấu hình JSON trong Python. .
Bước 1. Tạo tệp cấu hình JSON. Tạo tệp văn bản trống 'config. JSON' Thêm giá trị chiều rộng và chiều cao trong ký hiệu JSON như thế này. { "chiều rộng". 1024, "chiều cao". 768 }
Bước 2. Tạo tập lệnh Python. Tạo tệp văn bản trống 'loadconfig. py' và đặt nó vào cùng thư mục với 'config. JSON'

định dạng là gì. tập tin conf?

Tệp CONF là tệp cấu hình hoặc "config" được sử dụng trên các hệ thống dựa trên Unix và Linux . Nó lưu trữ các cài đặt được sử dụng để định cấu hình các quy trình và ứng dụng hệ thống. Các tệp CONF tương tự như. Tệp CFG được tìm thấy trên hệ thống Windows và Macintosh.

Chủ Đề