Deepak
Deepak

Reputation: 43

Single line commands from Python

I am trying to change certain entries in a file using python, which is possible in Perl with the command below , do we have anything similar in python, here the string in the file is replaced successfully.

[root@das~] perl -pi -w -e 's/unlock_time=1800/#unlock_time=1900/g;' /etc/pam.d/common-auth

For this i did try simple command in python to start off with, but no luck, any help in this direction would help, the code below does not give any output actually.

[root@das~] python -c 'import os ; os.uname()[1]'

Upvotes: 0

Views: 96

Answers (1)

hiro protagonist
hiro protagonist

Reputation: 46869

you need to add a print statement (with surrounding brackets for python 3.4; without for python 2.7).

[root@das~] python -c 'import os ; print(os.uname()[1])'

the other line could then be programmed this way (this will replace the input file!):

import fileinput

for line in fileinput.input('test.txt', inplace=True):
    if line.startswith('unlock_time'):
        print('# {}'.format(line))    
    else:
        print(line)

Upvotes: 1

Related Questions