I created a Python project that requires the camera to be enabled. In order to make it user friendly, I'm trying to enable the camera automatically without the user having to do it manually using raspi-config.
import configparser
def enable_camera():
config_path = '/boot/config.txt'
with open(config_path, 'r') as input_file:
config_string = '[default]\n' + input_file.read()
config = configparser.ConfigParser()
config.read_string(config_string)
config.set('default', 'start_x', '1')
config.set('default', 'gpu_mem', '128')
with open(config_path, 'w') as output_file:
config.write(output_file, space_around_delimiters=False)
After running the program this is the content of /boot/config/config.txt
[default]
dtparam=audio=on
start_x=1
gpu_mem=128
[pi4]
dtoverlay=vc4-fkms-v3d
max_framebuffers=2
[all]
configparser
because it requires having sections so I wrote my own custom parser.