473,714 Members | 2,531 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Rename files with numbers

I have one folder containing mp3 files, the folder is:
C:\My Shared Folder\Rubber Soul

And the files are:
01 drive my car.mp3
02 norwegian wood.mp3
03 you won't see me.mp3
04 nowhere man.mp3
..
..
..

I'm trying to rename files to:
The Beatles - Drive My Car.mp3
The Beatles - Norwegian Wood.mp3
The Beatles - You Won't See Me.mp3
The Beatles - Nowhere Man.mp3
..
..
..

So I need to change the file number to "The Beatles -" and Capitalize
the name.
I was trying to create a function and using glob and rename, but i had
no sucsses...
Could somebody help me please,
Since now, thanks...

Oct 31 '05 #1
16 2985
On Oct 31, du************@ gmail.com wrote:
I have one folder containing mp3 files, the folder is:
C:\My Shared Folder\Rubber Soul

And the files are:
03 you won't see me.mp3
.

I'm trying to rename files to:
The Beatles - You Won't See Me.mp3
.
My first suggestion is that you make better changes while you're
taking the effort to rename. I.e., don't use spaces or apostrophes
(or other shell-unfriendly characters) in file names (though some
might disagree with me on this religious issue). So for your case a
more parse-able/useful translation might be:

The_Beatles_-_You_Wont_See_M e.mp3
So I need to change the file number to "The Beatles -"
You'll probably want to use "re" for this. In a loop over
your glob'd files, something like:

re.sub(r'^\d\d\ s', r'The Beatles - ', ...)
and Capitalize the name.
If you avoid the apostrophe, then 'you wont see me'.title() will do
the Right Thing.
I was trying to create a function and using glob and rename, but i
had no sucsses... Could somebody help me please,


You should post the solution you've attempted to write if you want help
fixing it.

--
_ _ ___
|V|icah |- lliott http://micah.elliott.name md*@micah.ellio tt.name
" " """
Oct 31 '05 #2
Ok, so the function simplifyed without loops:

def renamer(folder, band):
archive = #file to transform
rest = archive[3:]
print band + " -",rest.capitali ze()
obs: the file names came this way(with spaces or apostrophes) from the
cd i imported.

Oct 31 '05 #3
On Oct 31, du************@ gmail.com wrote:
...
obs: the file names came this way(with spaces or apostrophes) from
the cd i imported.


So remove them first. Here's a possible solution::

#! /usr/bin/env python

import glob, os.path

uglies = glob.glob("*.mp 3")
print 'uglies:', uglies
pretties = []

for ugly in uglies:
song, ext = os.path.splitex t(ugly)
song = song.replace("' ", "")
song = song.replace(" ", "_")
song = song.title()
song = "The_Beatle s_-_" + song[3:]
song_ext = song + ext
pretties.append (song_ext)

print 'pretties:', pretties

# rename uglies to pretties...

--
_ _ ___
|V|icah |- lliott http://micah.elliott.name md*@micah.ellio tt.name
" " """
Oct 31 '05 #4
Micah, thanks a lot,
but my focus is to learn how to acess a folder and rename all files in
this folder.

Somyhing like this:
def renamer(folder, band):
folder = #place to act
archive = #file to transform
rest = archive[3:]
print band + " -", rest.capitalize ()

And than just do this:

renamer('C:\My Shared Folder\Rubber Soul', 'The Beatles')

And than pyhton alter all files in my folder. Is it possible?

I'm a beginer, for now, i just want to let the spaces and apostrophes
as they are...

Oct 31 '05 #5
On Oct 31, du************@ gmail.com wrote:
but my focus is to learn how to acess a folder and rename all files in
this folder.


This is a little more flexible than my last post, and it does the
whole job::

#! /usr/bin/env python

import glob, os, string

def fix_ugly_song_n ames(songdir, band, spacerepl='_'):
"""Rename an ugly MP3 file name to a beautified shell-correct
name. Only works for files named like "03 song name.mp3".
Use `spacerepl` to alter space replacement character.
WARNING: no error-handling!
"""
# Begin working in specified `songdir`.
os.chdir(songdi r)
# Shell-unfriendly characters made into a string.
badchars = ''.join( set(string.punc tuation) - set('-_+.~') )
# Identity table for later translation (removal of `badchars`).
tbl = string.maketran s('', '')
# MP3 files in `songdir` having ugly characters.
uglies = glob.glob("*.mp 3")
# Make some step-by-step changes to build `pretties` list.
pretties = []
for ugly in uglies:
song, ext = os.path.splitex t(ugly)
song = song.translate( tbl, badchars)
song = song.replace(" ", spacerepl)
song = song.title()
song = band+spacerepl+ "-"+spacerepl+son g[3:]
songext = song + ext
pretties.append (songext)
# Rename each file from ugly to pretty.
for ugly, pretty in zip(uglies, pretties):
if __debug__:
print ugly, '-->', pretty
else:
os.rename(ugly, pretty)

So for you to use spaces, just call with something like this::

fix_ugly_song_n ames('/var/mp3/pop/The_Beatles/Rubber_Soul',
'The Beatles',
spacerepl=' ')

I used the __debug__ gate to allow you to just see what it will do::

$ ls -1
01 drive my car.mp3
02 norwegian wood.mp3
03 you won't see me.mp3
04 nowhere man.mp3
mvmp3.py
$ python ./mvmp3.py
01 drive my car.mp3 --> The Beatles - Drive My Car.mp3
02 norwegian wood.mp3 --> The Beatles - Norwegian Wood.mp3
03 you won't see me.mp3 --> The Beatles - You Wont See Me.mp3
04 nowhere man.mp3 --> The Beatles - Nowhere Man.mp3

And then to actually do it with the non-__debug__ path::

$ python -O ./mvmp3.py
$ ls -1
mvmp3.py
The Beatles - Drive My Car.mp3
The Beatles - Norwegian Wood.mp3
The Beatles - Nowhere Man.mp3
The Beatles - You Wont See Me.mp3

If you want to keep the `badchars` in the file names, then you will
have to do *more* work since `song.title()` won't be able to do the
work for you.

Now I need to go beautify my collection. :-)

--
_ _ ___
|V|icah |- lliott http://micah.elliott.name md*@micah.ellio tt.name
" " """
Oct 31 '05 #6
On Oct 31, Micah Elliott wrote:
Now I need to go beautify my collection. :-)


While a fun exercise, there are probably already dozens (or
thousands?) of utilities in existence that do this and much more.

--
_ _ ___
|V|icah |- lliott http://micah.elliott.name md*@micah.ellio tt.name
" " """
Oct 31 '05 #7
Micah, thanks a lot, nice script!
But its not completly working, i ran this:

fix_ugly_song_n ames('C:\My Shared Folder\Rubber Soul', 'The
Beatles', spacerepl=' ')

And the shell prints this:

01 drive my car.mp3 --> The Beatles - Drive My Car.mp3
02 norwegian wood (this bird has flo).mp3 --> The Beatles -
Norwegian Wood This Bird Has Flo.mp3
03 you won't see me.mp3 --> The Beatles - You Wont See Me.mp3
04 nowhere man.mp3 --> The Beatles - Nowhere Man.mp3
05 think for yourself.mp3 --> The Beatles - Think For Yourself.mp3
06 the word.mp3 --> The Beatles - The Word.mp3
07 michelle.mp3 --> The Beatles - Michelle.mp3
08 what goes on.mp3 --> The Beatles - What Goes On.mp3
09 girl.mp3 --> The Beatles - Girl.mp3
10 i'm looking through you.mp3 --> The Beatles - Im Looking Through
You.mp3
11 in my life.mp3 --> The Beatles - In My Life.mp3
12 wait.mp3 --> The Beatles - Wait.mp3
13 if i needed someone.mp3 --> The Beatles - If I Needed
Someone.mp3
14 run for your life.mp3 --> The Beatles - Run For Your Life.mp3

But the files didn't change in the folder!
Is there something mising or i'm doing something worng ??

Nov 1 '05 #8
Oh, forget what i've just said, i didn't read the __debug__ part.
It worked perfectly =]
Realy good Python classes!
_______________ _____
Eduardo Figueiredo
http://dudufigueiredo.com

Nov 1 '05 #9
On 31 Oct 2005 10:52:27 -0800, du************@ gmail.com declaimed the
following in comp.lang.pytho n:
obs: the file names came this way(with spaces or apostrophes) from the
cd i imported.
Fancy CD, in that case... Most of mine come in as track01, track02,
etc. It is only when the ripping software accesses a CDDB system that
real titles get assigned (and the track number is an option in the
current software I have).

However...
def renme(artist, track): .... l1 = artist.title(). split()
.... l2 = track.title().s plit()[1:]
.... return "_".join(l1 + l2)
.... fn '01 - some title.mp3' renme("the beatles", fn) 'The_Beatles_-_Some_Title.Mp3 '
Or, if you are really insane... <G>
fn '01 - some title.mp3' "_".join("t he beatles".title( ).split() + fn.title().spli t()[1:]) 'The_Beatles_-_Some_Title.Mp3 '

-- =============== =============== =============== =============== == <
wl*****@ix.netc om.com | Wulfraed Dennis Lee Bieber KD6MOG <
wu******@dm.net | Bestiaria Support Staff <
=============== =============== =============== =============== == <
Home Page: <http://www.dm.net/~wulfraed/> <
Overflow Page: <http://wlfraed.home.ne tcom.com/> <

Nov 1 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
1803
by: Erik Foreman | last post by:
I am using a folderbrowserdialog object to prowse to a folder then once in that folder I am going to have my program rename all of the files in that folder. I know the rename funchtion but I am not sure how to get the program to loop through each file in the folder so they all get renamed in a certian sequence. can someone help me? *** Sent via Developersdex http://www.developersdex.com ***
5
4460
by: Rothariger | last post by:
Hello.... i want to know if its posible to rename multiple files like windows does.. example: file zzzzzzz.doc file asdasd.doc file esfsefse.doc
3
5375
by: Yannick Benoit | last post by:
Hi guys. I have like hundreds of pictures in a folder and then I use this function to rename them all to a following number. but very often I noticed that some pictures get lost and I dont know where ? anyone can tell me whats wrong ? $dir = getcwd()."/dump/"; $dh = opendir($dir);
6
2530
by: shuaishuaiyes | last post by:
Hello everyone... I'm a Chinese student and my English is very poor...So excuse me if I make grammar mistake. I want to ask some questions about "rename". I'm a beginner, so my C ..... :) I want to rename a batch of files..Such as I want to rename"1.avi,2.avi" to "Prison BreakS1E1.avi,Prison BreakS1E2.avi" in the same folder..How can I do?
2
3628
by: =?iso-8859-1?b?cultaQ==?= | last post by:
Hi, I would like to rename files (jpg's ones) using a text file containing the new names... Below is the code that doesn't work : ***** #!/usr/bin/python #-*- coding: utf-8 -*- from os import listdir, getcwd, rename import re
1
1615
by: lukas | last post by:
hello, i recently had the job of having to rename about 200 files. The source for the renaming was a bunch of names in a file. I know next to nothing when it comes to bash scripting (which would have seemed the obvious choice) so i used python. The files i was renaming were canon raw files (.CR2). my problem is that after i rename the files, OS X will no longer render the thumbnails for the files, and preview is no longer the default...
6
1742
by: Alexandra | last post by:
Hi, I have a folder with hundreds of text files. Each text file contains a row with a date. I want to rename the text file with the date contained in this text file. Example: Suppose I have 2 textfiles, then one text file would contain a row like this: DATE 1 February 2008 and the other would contain a row like this: DATE 2 February 2008
2
1720
by: newyorker213 | last post by:
Hello, We have files ( named in the following format ) in one of our folders: MV0001.XLS MV0002.XLS and so on WV0001.XLS WV0002.XLS
1
5716
by: achotto | last post by:
hi, i try to upload a multiple image files. after that i will rename the files name. the problem is when i upload a 2 or more same files name exp-goal.jpg, it will return "files already exist". ok this is my codes, Set Upload = Server.CreateObject("Persits.Upload.1") Count = Upload.Save("d:\cmsupload\cimg\") for each File in upload.Files ' '...sme operation here..
0
8796
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8704
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9307
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9170
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9009
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7946
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4715
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3155
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2514
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.