[Tutor] renaming files within a directory

Alan Gauld alan.gauld at btinternet.com
Mon Jul 27 11:22:57 CEST 2009

<davidwilson at Safe-mail.net> wrote

> The problem is that for example the file Flag_of_the_United_States.svg 
> when I use the strip('.svg') it is returned as Flag_of_the_United_State

Thats because strip removes all of the characters in the given string.

You would probably be better removing the extension using the 
os.path.splitext() function.

> Also, How do I remove 'Flag_of', 'Flag_of_the_' 

You could try replacing with a null string. Do the second one first 
followed by the first if necessary, using the string.replace() method.


Alan Gauld
Author of the Learn to Program web site

More information about the Tutor mailing list