[Tutor] renaming files within a directory

davidwilson at Safe-mail.net davidwilson at Safe-mail.net
Sun Jul 26 18:08:50 CEST 2009


OK, I know that.

Here is what I have so far, but I get stuck on the mapping of the filename with the csv file.

>>> import os
>>> files = [file for file in os.listdir(os.getcwd()) if file.endswith('svg')]
>>> files
['Flag_of_Abkhazia.svg', 'Flag_of_Afghanistan.svg', 'Flag_of_Albania.svg', 'Flag_of_Algeria.svg', 'Flag_of_Andorra.svg', 'Flag_of_Angola.svg', 'Flag_of_Antigua_and_Barbuda.svg', 'Flag_of_Argentina.svg', 'Flag_of_Armenia.svg', 'Flag_of_Australia.svg']

>>> import csv
>>> reader = csv.reader(open("country.csv"))
>>> for row in reader:
...     print row   

['"km";"Comoros"']
['"dj";"Djibouti"']
['"er";"Eritrea"']
['"et";"Ethiopia"']
....

Here is where I am at.
Not sure how to map the two.

Dave





-------- Original Message --------
From: Tim Golden <mail at timgolden.me.uk>
Apparently from: tutor-bounces+davidwilson=safe-mail.net at python.org
To: 
Cc: tutor at python.org
Subject: Re: [Tutor] renaming files within a directory
Date: Sun, 26 Jul 2009 16:13:38 +0100

> davidwilson at Safe-mail.net wrote:
> > Hello,
> > I have a directory containing svg/Flag_of_United_States.svg etc...
> > 
> > 
> > Flag_of_the_Seychelles.svg
> > Flag_of_the_Solomon_Islands.svg
> > Flag_of_the_United_Arab_Emirates.svg
> > Flag_of_the_United_Kingdom.svg
> > Flag_of_the_United_States.svg
> > Flag_of_the_Vatican_City.svg
> > 
> > 
> > Also I have a CSV file like
> > 
> > "us", "United States"
> > "es", "Spain"
> > 
> > ....
> > 
> > How can I rename the svg files so that they become:
> > 
> > flag-us.svg
> > flag-es.svg
> 
> 
> This is one of those ones where it helps if you have
> a go first and tell us how far you got, or where
> the problem lies. Otherwise you're likely to get
> a lot of sarcastically helpful replies along the lines
> of:
> 
> move Flag_of_the_Seychelles.svg flag-sc.svg
> 
> 
> To get you started if you're nowhere at the moment,
> you want to create a dictionary from your csv which maps 
> the long name to the short name so that can then
> parse the file name to find the long name, use the
> dictionary to find the short name, and then issue
> a rename command to the new name.
> 
> TJG
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor


More information about the Tutor mailing list