[Tutor] understanding fileinput

Michele Alzetta michele.alzetta at aliceposta.it
Wed Apr 7 16:12:13 EDT 2004

I recently tried to solve a problem I had: changing a certain pattern of
text within all files in a certain directory and changing any filename
with the same pattern in the same fashion.
[Note: I have solved my actual problem with the judicious use of sed + a
bash script called renna - and secondly by finding the correct option of
wget to use when downloading asp web pages   :-)  ]

However, it seemed like an interesting excercise in learning python. At
first I thought it ought to be very easy to code this, but it isn't
really for a newbie like me.

This is what I've come up with at the moment:

import os, sys, re, fileinput

    target_folder = (sys.argv[1])
    original_pattern = (sys.argv[2])
    result_pattern = (sys.argv[3])
    print "\n Substitutes a string with another in all files of a
specified directory and its subdirectories"
    print "Use: ./MyScript.py directory string other_string"
for folders, folder, filelist in os.walk(target_folder):
    for filename in filelist:
        file = os.path.join(folders,filename)
        for line in fileinput.input(file,'inplace=1'):
            line = re.sub(original_pattern,result_pattern,line)
            print line   
#        fileinput.close()

I haven't yet tried to add the code for changing filenames but it seems
pretty straightforward: re.sub + os.rename, I suppose.

Two questions here:

- this correctly changes the pattern in filecontent but for some reason
adds a newline for every newline originally present in the file at every
pass - why ?

- from documentation of fileinput I thought fileinput.close() was
necessary, but actually commenting it out doesn't change anything for
good or ill in the working of the program.

Michele Alzetta <michele.alzetta at aliceposta.it>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Questa parte del messaggio =?ISO-8859-1?Q?=E8?= firmata
Url : http://mail.python.org/pipermail/tutor/attachments/20040407/6c65afea/attachment.bin

More information about the Tutor mailing list