[Tutor] Substitution question...

antonmuhin at rambler.ru antonmuhin at rambler.ru" <antonmuhin@rambler.ru
Wed Feb 12 02:40:01 2003


Hello montana,

Wednesday, February 12, 2003, 7:34:31 AM, you wrote:

m> Hi Everyone-

m> I Would like to open a list of files an re-write them all to new filenams.  I know how to do this with a for loop and then open each file for reading and then writing them to a new file name.  My
m> question deals more with substitution in the filenames. For insatnce:

m> I know that all of the files contain  five letters followed by a number that couls be 1or 2 digits followed by .html. --> xxxxx1.html or xxxxx12.html

m> What I would like to do is lopp off the 'xxxxx' and replace with 'yyy' so the previous example would end up with yyy1.html and yyy12.html.

m> (of course the '1' and '12' could be any number that is between 1 and 2 digits long)

m> How can I accomplish this easily.

m> Thanks.
m> SA
m> :)

If your files' names has such a strict structutre you described above,
you might even keep away from regexp's:

name = "xxxxx12"

name[:5] will produce "xxxxx", while name[5:]---"12"

Therefore to substitute you may write:

newName = "yyy" + name[5:]

BTW, if you need just to copy or rename files, you might be better
with os.rename and shutil.* functions.

-- 
Best regards,
 anton                            mailto:antonmuhin@rambler.ru