Thank you all for your help. -------- Original Message -------- From: Dave Angel <da...@ieee.org> Apparently from: srs0=pajg=du=ieee.org=da...@srs.perfora.net To: davidwil...@safe-mail.net Cc: ken...@tds.net, tutor@python.org Subject: Re: Re: [Tutor] renaming files within a directory Date: Mon, 27 Jul 2009 09:01:58 -0400
> davidwil...@safe-mail.net wrote: > > Here is the updated viersion: > > > > --- > > > > import glob > > import csv > > from os import rename > > > > > > countries =} > > reader =sv.reader(open("countries.csv")) > > for row in reader: > > code, name =ow > > countries[name] =ode > > > > files =et([file for file in glob.glob('Flag_of_*.svg')]) > > > > for file in files: > > file =ile[8:-4] > > if file.startswith('the_'): > > file =ile[4:] > > if countries.has_key(file): > > b =flag-'+ countries[file] + '.svg' > > print b > > rename(file, b) > > > > But I cannot get the rename to take effect and I get an error: > > > > $ python rename_svg.py > > Uganda flag-ug.svg > > Traceback (most recent call last): > > File "rename_svg.py", line 21, in <module> > > rename(file, b) > > OSError: [Errno 2] No such file or directory > > > > What am I missing? > > > > -------- Original Message -------- > > From: Kent Johnson <ken...@tds.net> > > Apparently from: kent3...@gmail.com > > To: davidwil...@safe-mail.net > > Cc: m...@timgolden.me.uk, tutor@python.org > > Subject: Re: [Tutor] renaming files within a directory > > Date: Mon, 27 Jul 2009 07:01:32 -0400 > > > > > >> On Mon, Jul 27, 2009 at 5:10 AM, <davidwil...@safe-mail.net> wrote: > >> > >> > >>> files =et([file for file in os.listdir(os.getcwd()) if > >>> file.endswith('svg')]) > >>> print len(files) > >>> > >>> for file in files: > >>> file =ile.strip('.svg') > >>> print file > >>> # if countries.has_key(file): > >>> # print file > >>> > >>> When I run this I get: > >>> > >>> Flag_of_Uganda > >>> ... > >>> > >>> 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 > >>> > >>> Also, How do I remove 'Flag_of', 'Flag_of_the_' > >>> > >> I suggest you use glob.glob() instead of os.listdir(): > >> > >> files =lob.glob('Flag_of_*.svg) > >> > >> Then you know that each file name starts with Flag_of_ and ends with > >> .svg. To remove them, since they are fixed strings you can just use > >> slicing; > >> file =ile[8:-4] > >> if file.startswith('the_'): > >> file =ile[4:] > >> > >> Kent > >> > > > > (Please don't top-post on this mailing list. It hopelessly confuses which > > order the quoted messages come.) > > > > One reason the rename() will fail is that you're changing file between > the for loop and the rename. Incidentally, file is a lousy name to use, > since it already has a meaning in the std lib. It's the type of the > object you get from open(), or from glob.glog(). > > > I didn't check the rest, but for this change, you'd get: > > for infile in files: > country = infile[8:-4] > if country.startswith('the_'): > country = country[4:] > if countries.has_key(file): > b = 'flag-'+ countries[country] + '.svg' > print b > rename(file, b) > > > BTW, several other characters were dropped in your email. Did you > retype the code (bad), or use cut/paste? I just cut and paste from vim. Here is the final code: import glob import csv from os import rename countries = {} reader = csv.reader(open("countries.csv")) for row in reader: code, name = row countries[name] = code files = set([file for file in glob.glob('Flag_of_*.svg')]) for filename in files: fn = filename[8:-4] if fn.startswith('the_'): fn = fn[4:] if fn in countries: new_filename = 'flag-%s.svg' % countries[fn] rename(filename, new_filename) > > DaveA _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor