开发者

Add line on top of each Python file in current and sub directories

I'm on an 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 to the top of each .py file. What's the easiest way to do that using Perl, Python, or shell开发者_如何学运维 script?


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

Edit: from tchrist's comment, handle filenames with spaces.

Assuming you have GNU find and xargs (as you specified the linux tag on the question)

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

Without GNU tools, you'd do something like:

while IFS= read -r filename; do
  { echo "new line"; cat "$filename"; } > tmpfile && mv tmpfile "$filename"
done < <(find . -name \*.py -print)


for a in `find . -name '*.py'` ; do cp "$a" "$a.cp" ; echo "Added line" > "$a" ; cat "$a.cp" >> "$a" ; rm "$a.cp" ; done


    #!/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 recursively run this command in your shell:

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


This will

  1. recursively walk all directories starting with the current working directory
  2. modify only those files whose filename end with '.py'
  3. preserve file permissions (unlike open(filename,'w').)

fileinput also gives you the option of backing up your original 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)


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 can't insert in begining or end of file in python. Only re-write or append.


What's the easiest way to do that using Perl, Python, or shell script?

I'd use Perl, but that's because I know Perl much better than I know Python. Heck, maybe I'd do this in Python just to learn it a bit better.

The easiest way is to use the language that you're familiar with and can work with. And, that's also probably the best way too.

If these are all Python scripts, I take it you know Python or have access to a bunch of people who know Python. So, you're probably better off doing the project in Python.

However, it's possible with shell scripts too, and if you know shell the best, be my guest. Here's a little, completely untested shell script right off the top of 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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜