0

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]'

1 Answer 1

1

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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.