Answer the question
In order to leave comments, you need to log in
How to change the contents of /etc/dhcpcd.conf using Python?
Good afternoon, in the /etc/dhcpcd.conf file, the ip address of Raspberry is statically set, for example:
interface eth0
static ip_address=192.168.0.10/24
static routers=192.168.0.1
static domain_name_servers=192.168.0.1
interface eth0
static ip_address=192.168.0.18
static routers=192.168.0.1
static domain_name_servers=192.168.0.1
Answer the question
In order to leave comments, you need to log in
for example like this:
def file_edit(fname, field, new_value):
""" edit conf file <field> value to new one """
if type(fname) != str | \
type(field) != str | \
type(new_value) != str :
return False
""" to do - validate the field mask """
with open(fname, 'r') as f:
file = f.read()
strs = file.split('\n')
for i, _str in enumerate(strs):
if field & not "#" in _str: #don't edit comments
strs[i] = field + new_value
out = ''
for _str in strs:
out+=_str+'\n'
with open(fname, 'w') as f:
f.write(out)
return True
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question