Editing of python files

Asked

Viewed 84 times

3

I intend to insert in files .py the stretch below:

-*- encoding: utf-8 -*-

needs to be in specific line, and in large amount of files.

Is there any Unix/linux tool that can perform this task?

  • Something ready I don’t think there is, but I can do this with the sed or create a script in Perl.

  • I looked for something in sed and did not find. I also researched something in awk.

  • It has to be in a specific arbitrary line or precisely in the first line?

  • It has to be on the second line. @rodorgas

1 answer

2


To insert this line, use the command sed:

sed -i '2i-*- encoding: utf-8 -*-' arquivo.py

  • -i saves the edits in the file itself
  • 2i insere the string in the 2nd line

To do this in all python files in the same folder, replace arquivo.py for *.py. You can use other wildcard characters supported.

  • I was failing to use an old version of sed. Worked great!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.