Re: hlep: a text search and rename question

2009-01-12 Thread Steve Holden
sensen wrote:
> On Jan 12, 10:41 pm, sensen  wrote:
>> both above works well, but a problem is renamed file is without
>> filename extension.
>>
>> only change to the title.
>>
>> for example, the origin file is track_1.flac, after run the script i
>> want it to for example, White Flag.flac, but now it change to White
>> Flag without extension.
>>
>> could you do a favor to fix it. thank you.
> 
> fixed it, i check the reference of python.
> the last line  from
>os.rename(old, new)
> changed to
>os.rename(old, new+'.flac')
> 
> thanks, now i still try to read the reference of python.
> i want to the script can know what file it will open. in other words,
> the script maybe first do a search, and get the exactly file name of
> the cue type file in the folder, then open it.

Well done - the true Python spirit. Fix it yourself when you can. And
now you know a bit more about the logic James so kindly wrote for you.
Congratulations, and welcome to the Python community.

regards
 Steve

-- 
Steve Holden+1 571 484 6266   +1 800 494 3119
Holden Web LLC  http://www.holdenweb.com/

--
http://mail.python.org/mailman/listinfo/python-list


Re: hlep: a text search and rename question

2009-01-12 Thread sensen
On Jan 12, 10:41 pm, sensen  wrote:
> both above works well, but a problem is renamed file is without
> filename extension.
>
> only change to the title.
>
> for example, the origin file is track_1.flac, after run the script i
> want it to for example, White Flag.flac, but now it change to White
> Flag without extension.
>
> could you do a favor to fix it. thank you.

fixed it, i check the reference of python.
the last line  from
   os.rename(old, new)
changed to
   os.rename(old, new+'.flac')

thanks, now i still try to read the reference of python.
i want to the script can know what file it will open. in other words,
the script maybe first do a search, and get the exactly file name of
the cue type file in the folder, then open it.
--
http://mail.python.org/mailman/listinfo/python-list


Re: hlep: a text search and rename question

2009-01-12 Thread sensen
both above works well, but a problem is renamed file is without
filename extension.

only change to the title.

for example, the origin file is track_1.flac, after run the script i
want it to for example, White Flag.flac, but now it change to White
Flag without extension.

could you do a favor to fix it. thank you.
--
http://mail.python.org/mailman/listinfo/python-list


Re: hlep: a text search and rename question

2009-01-12 Thread sensen
On Jan 12, 8:17 pm, James Stroud  wrote:
> James Stroud wrote:
> > cue = iter(open('CDImage.cue').readlines())
>
> It just occurred to me that this could simply be:
>
> cue = open('CDImage.cue')
>
> James
>
> --
> James Stroud
> UCLA-DOE Institute for Genomics and Proteomics
> Box 951570
> Los Angeles, CA 90095
>
> http://www.jamesstroud.com

Thanks you James, i will check it now. thanks again for your kindness
help.
--
http://mail.python.org/mailman/listinfo/python-list


Re: hlep: a text search and rename question

2009-01-12 Thread James Stroud

James Stroud wrote:

cue = iter(open('CDImage.cue').readlines())


It just occurred to me that this could simply be:

cue = open('CDImage.cue')

James


--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com
--
http://mail.python.org/mailman/listinfo/python-list


Re: hlep: a text search and rename question

2009-01-12 Thread James Stroud

sensen wrote:

matter description:

when a use an tools to do the ape to flac convert, i can use the cue
file attached with ape, but the problem is the converted flac file
don't name by the title in the cue file but like Track_1.flac,
Track_2.flac ... , so i want to write a script to do this work, system
is xp sp2, the python is 2.44, i have a plone site in this computer,
that is why i want to use python to do this work.

so simplify and example

CDImage.cue, Track_1.flac, Track_2.flac, Track_3.flac, Track_4.flac
all above files in the same folder.

the cue file is just a text file like bellow:


PERFORMER "Dido"
TITLE "Life For Rent"
FILE "Dido - Life for Rent.ape" WAVE
  TRACK 01 AUDIO
TITLE "White Flag"
PERFORMER "Dido"
INDEX 01 00:00:00
  TRACK 02 AUDIO
TITLE "Stoned"
PERFORMER "Dido"
INDEX 00 04:00:03
INDEX 01 04:01:45
  TRACK 03 AUDIO
TITLE "Life For Rent"
PERFORMER "Dido"
INDEX 00 09:56:47
INDEX 01 09:56:53
  TRACK 04 AUDIO
TITLE "Mary's In India"
PERFORMER "Dido"
INDEX 01 13:37:57

the things i want to do

1. search the current folder cue file (only one cue file) contents.
find the TITLE "White Flag" and get the White Flag and maybe we
can save it to a var.
2. then rename the Track_1.flac to the White Flag.flac
3. search the next title TITLE "Stoned" and save ti to var.
4. then rename the Track_2.flac to the Stoned.flac.
5. do the same things to the end title.

someone can give some outline, or code is more wonderful.  thanks in
advance.


This isn't much work in python:

import os, glob, re
cue = iter(open('CDImage.cue').readlines())
titles = []
for line in cue:
  if line.strip().startswith('FILE'):
break
for line in cue:
  if line.strip().startswith('TITLE'):
titles.append(line.split('"')[-2])
flacs = glob.glob('*.flac')
flacs.sort(key=lambda f: int(re.search(r'\d+', f).group(0)))
for old, new in zip(flacs, titles):
  os.rename(old, new)


There is no error checking for properly formatted cue file, etc.

James

--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com
--
http://mail.python.org/mailman/listinfo/python-list


hlep: a text search and rename question

2009-01-11 Thread sensen
matter description:

when a use an tools to do the ape to flac convert, i can use the cue
file attached with ape, but the problem is the converted flac file
don't name by the title in the cue file but like Track_1.flac,
Track_2.flac ... , so i want to write a script to do this work, system
is xp sp2, the python is 2.44, i have a plone site in this computer,
that is why i want to use python to do this work.

so simplify and example

CDImage.cue, Track_1.flac, Track_2.flac, Track_3.flac, Track_4.flac
all above files in the same folder.

the cue file is just a text file like bellow:


PERFORMER "Dido"
TITLE "Life For Rent"
FILE "Dido - Life for Rent.ape" WAVE
  TRACK 01 AUDIO
TITLE "White Flag"
PERFORMER "Dido"
INDEX 01 00:00:00
  TRACK 02 AUDIO
TITLE "Stoned"
PERFORMER "Dido"
INDEX 00 04:00:03
INDEX 01 04:01:45
  TRACK 03 AUDIO
TITLE "Life For Rent"
PERFORMER "Dido"
INDEX 00 09:56:47
INDEX 01 09:56:53
  TRACK 04 AUDIO
TITLE "Mary's In India"
PERFORMER "Dido"
INDEX 01 13:37:57

the things i want to do

1. search the current folder cue file (only one cue file) contents.
find the TITLE "White Flag" and get the White Flag and maybe we
can save it to a var.
2. then rename the Track_1.flac to the White Flag.flac
3. search the next title TITLE "Stoned" and save ti to var.
4. then rename the Track_2.flac to the Stoned.flac.
5. do the same things to the end title.

someone can give some outline, or code is more wonderful.  thanks in
advance.
--
http://mail.python.org/mailman/listinfo/python-list