[Tutor] renaming files within a directory

Tim Golden mail at timgolden.me.uk
Mon Jul 27 11:21:13 CEST 2009


davidwilson at Safe-mail.net wrote:
> Here is what I have so far:
> 
> import os
> import csv
> 
> countries = {}
> reader = csv.reader(open("countries.csv"))
> for row in reader:
>     code, name = row
>     countries[name] = code
> 
> files = set([file for file in os.listdir(os.getcwd()) if file.endswith('svg')])

You don't really need the set () here, as I doubt your operating
system allows two files of the same name in the same directory.
Also, you can use os.listdir (".") if you want.

> print len(files)
> 
> for file in files:
>     file = file.strip('.svg')
>     print file
> #    if countries.has_key(file):
> #	print file
> 

[... snip ...]

> 
> 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

Yes, this is a common gotcha when using strip. Check out the
docs of strip:

"""
Help on built-in function strip:

strip(...)
    S.strip([chars]) -> string or unicode

    Return a copy of the string S with leading and trailing
    whitespace removed.
    If chars is given and not None, remove characters in chars instead.
    If chars is unicode, S will be converted to unicode before stripping
"""

The parameter to strip -- if given -- is a string of chars,
any and all of which are stripped from the ends of the
string. It is not a *string* which is stripped in its
entirety; it is a list of characters. So here, Python is
stripping any of ".", "s", "v", "g" from the ends of your
string.

I suggest you put together a little function, eg:

def rstrip_string (look_in, look_for):
  if look_in.endswith (look_for):
    return look_in[:-len (look_for)]
  else:
    return look_in

which you can then call:

files = [rstrip_string (f, ".svg") for f in files]

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

Same sort of idea.

> 
> I guess after this I can compare the value with the key and map the tld?
> 
> Or is it better to use regex and then search from the list of countries? But how???

regex would be possible here, but probably overkill.
You're very nearly there. Assuming the format of the
strings is consistently simple, you can take shortcuts
as above. If it's possible that one file has "flag_of"
while another has "flag-of" and another "flags-of" then
you might need to get fancier.


TJG


More information about the Tutor mailing list