Add a line on top of each Python file in current and subdirectories

I am on the Ubuntu platform and have a directory containing many .py files and subdirectories (also containing .py files). I would like to add a line of text at the beginning of each .py file. The easiest way to do this using Perl, Python, or a shell script?

+7
source share
6 answers
#!/usr/bin/perl use Tie::File; for (@ARGV) { tie my @array, 'Tie::File', $_ or die $!; unshift @array, "A new line"; } 

To process all .py files in a directory, execute this command recursively in your shell:

find . -name '*.py' | xargs perl script.pl

+4
source
 find . -name \*.py | xargs sed -i '1a Line of text here' 

Edit: from tchrist comment, handle file names with spaces.

Assuming you have a GNU search and xargs (as you pointed the linux tag to the question)

 find . -name \*.py -print0 | xargs -0 sed -i '1a Line of text here' 

Without GNU tools, you would do something like:

 while IFS= read -r filename; do { echo "new line"; cat "$filename"; } > tmpfile && mv tmpfile "$filename" done < <(find . -name \*.py -print) 
+6
source
 for a in `find . -name '*.py'` ; do cp "$a" "$a.cp" ; echo "Added line" > "$a" ; cat "$a.cp" >> "$a" ; rm "$a.cp" ; done 
+5
source
 import os for root, dirs, files in os.walk(directory): for file in files: if file.endswith('.py') file_ptr = open(file, 'r') old_content = file_ptr.read() file_ptr = open(file, 'w') file_ptr.write(your_new_line) file_ptr.write(old_content) 

As far as I know, you cannot insert the beginning or end of a file in python. Only rewrite or add.

+4
source

This will

  • recursively go through all directories starting from the current working directory
  • Only change files whose filename ends with ".py"
  • save file permissions (unlike open(filename,'w') .)

fileinput also gives you the ability to back up source files before modifying them.


 import fileinput import os import sys for root, dirs, files in os.walk('.'): for line in fileinput.input( (os.path.join(root,name) for name in files if name.endswith('.py')), inplace=True, # backup='.bak' # uncomment this if you want backups ): if fileinput.isfirstline(): sys.stdout.write('Add line\n{l}'.format(l=line)) else: sys.stdout.write(line) 
+4
source

What is the easiest way to do this using Perl, Python or a shell script?

I would use Perl, but that is because I know Perl much better than I know Python. Damn, maybe I'll do it in Python to find out a little better.

The easiest way is to use a language that you are familiar with and can work with. And that, too, is probably the best way.

If these are all Python scripts, I understand that you know Python or have access to a group of people who know Python. So you are probably doing a better project in Python.

However, this is also possible with shell scripts, and if you know what the best shell is, be my guest. Here's a little, completely untested shell script right from my head:

 find . -type f -name "*.py" | while read file do sed 'i\ I want to insert this line ' $file > $file.temp mv $file.temp $file done 
+1
source

All Articles