Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

how to change pi hostname in python file

    import os
    def setHostname(newhostname):
        with open('/etc/hosts', 'r') as file:
        # read a list of lines into data
        data = file.readlines()

        # the host name is on the 6th line following the IP address
        # so this replaces that line with the new hostname
        data[5] = '127.0.1.1       ' + newhostname

        # save the file temporarily because /etc/hosts is protected
        with open('temp.txt', 'w') as file:
            file.writelines( data )

        # use sudo command to overwrite the protected file
        os.system('sudo mv temp.txt /etc/hosts')

        # repeat process with other file
        with open('/etc/hostname', 'r') as file:
            data = file.readlines()

        data[0] = newhostname

        with open('temp.txt', 'w') as file:
            file.writelines( data )

        os.system('sudo mv temp.txt /etc/hostname')

    #Then call the def
    setHostname('whatever')
Comment

PREVIOUS NEXT
Code Example
Python :: python print statements 
Python :: python dash bootstrap buttons with icons 
Python :: reddit python 3 time a python program 
Python :: double char 
Python :: in np array how to make element as 1 if it exceeds the threshold 
Python :: how to install pandas in python by git 
Python :: how to write flow of execution in python 
Python :: torch.cuda.randn 
Python :: corresponding angles 
Python :: cudf - merge dataframes 
Python :: grouped bar chart with labels 
Python :: install sort 
Python :: multivariable traces f(x, y) = sin(x)cos(y) 
Python :: numpy symmetrize array 
Python :: Tape Equilibrium 
Python :: mo.group() separated with spaces instead of commas python 
Python :: python if statement syntax 
Python :: python herencia clases 
Python :: osmapi 
Python :: keras imagenet 
Python :: nvidia-smi with user name 
Python :: put legend in subplot 
Python :: ploting to data on the same axis 
Python :: disable gpu in jupyter notebook 
Python :: python project structure 
Python :: HIDING AND ENCRYPTING PASSWORDS IN PYTHON USING MASKPASS MODULE 
Python :: connection to python debugger failed: socket closed 
Python :: what will be the output of the following python code? x = 123 for i in x: print(i) 
Python :: Create multiple lists with defined shape filled with 0 
Python :: python setup specify c++ version 
ADD CONTENT
Topic
Content
Source link
Name
2+2 =