How to Read and Write Ini File with Python3

How to read and write INI file with Python3?

This can be something to start with:

import configparser

config = configparser.ConfigParser()
config.read('FILE.INI')
print(config['DEFAULT']['path']) # -> "/path/name/"
config['DEFAULT']['path'] = '/var/shared/' # update
config['DEFAULT']['default_message'] = 'Hey! help me!!' # create

with open('FILE.INI', 'w') as configfile: # save
config.write(configfile)

You can find more at the official configparser documentation.

How to read and get the stored value from ini file in python script?

Make ini file like this:

[db]
server=raj
log=ere2
[Auth]
login=hi

and import like:

import configparser
config= configparser.ConfigParser()
config.read(r'C:\Users\PycharmProjects\Integration\local.ini')
server = config['db']['server')]

Or if you want the returned data to always be str, use:

server = str(config['db']['server')])

How To Read and Write to a INI file in Python

"Is there a way to only rewrite the value I am changing?" No, because it's a text file. You can only do a selective rewrite when you know that the thing you're writing will be exactly the same length as the thing you're replacing. That's not generally the case with text files, and they're almost never treated that way.

I'd do only a small re-organization of this function to remove redundancy:

def read_or_write_file(self, file, section, passed_option = None,
value = None, read = True):

config = ConfigParser.RawConfigParser()
with open(file) as configfile:
config.readfp(configfile)

if read:
options = config.options(section)

for option in options:
file_settings[option] = config.get(section, option)

else:
config.set(section, passed_option, value)

with open(file, 'w') as configfile:
config.write(configfile)

How can I read INI file in Python without configparser?

Have you tried just opening it as a json file? Since the ini file is a text file, it should * just work *

import json

# I saved your example in a text file called test.ini and
# put it in the same folder as this script
with open('test.ini', 'r') as f:
data = json.load(f)

The contents of your ini file will be in data, which will be a dictionary.

Reading .ini files in Python

You should specify the actual path to your .ini file instead of 'The_Path_to_ini' at these lines:

    f = open('The_Path_to_ini', 'r')
print(self.iniConfig.read('The_Path_to_ini'))

It may also be a good idea to handle an exception that may occur in open call. According open() documentation:

If the file cannot be opened, an OSError is raised.

You can handle this exception using try ... except block by this way:

try:
f = open('file', 'r')
...
except OSError as e:
print('File cannot be opened: ', e)

It is common to use with statement instead of try ... except blocks if you don't need to handle exceptions. For example:

with open('file', 'r') as f:
# work with f

Read more about with statement in Python documentation.

Read and write a config file with Python3 without losing the comments in the file

The ConfigObj is the solution.

How to read config(.ini) file in python which will work on 2.7 and 3.x python

You can make use of configparser backport, so it will work on both Python version.

pip install configparser


Related Topics



Leave a reply



Submit