Remove spaces and convert capitals to lower case

This is the place for queries that don't fit in any of the other categories.

Remove spaces and convert capitals to lower case

Postby choptrees » Wed Apr 02, 2014 9:27 pm

I'm trying to remove spaces from folder names in a directory, I'm also trying to find capital letters and repalcing them with lower case. I have been working on a script below, which works if I comment out the code blocks to run independtly of each other. My end goal is to add more code to remove non-alphanumeric characters. Any help for a newbie on this script much appreicated:

Code: Select all
import os

inDir = r'M:\WORKSPACE\Test'

for (r,d,f) in os.walk(inDir):
    for fld in d:
        curDir1 = os.path.join(r,fld)
        os.rename(curDir1, curDir1.replace(' ', '_'))
        print curDir1
        print 'Replaced all spaces with underscores'
       

for (r,d,f) in os.walk(inDir):
    for flr in d:
        curDir2 = os.path.join(r,flr)
        os.rename(curDir2, curDir2.lower())
        print curDir2 + ' this is #2'
        print 'Replaced all capitals with lower case'
Last edited by stranac on Wed Apr 02, 2014 9:32 pm, edited 1 time in total.
Reason: First post lock.
choptrees
 
Posts: 5
Joined: Wed Apr 02, 2014 9:23 pm

Re: Remove spaces and convert capitals to lower case

Postby stranac » Wed Apr 02, 2014 9:41 pm

So what's the problem?
Do you get an error, or does it not work as you expected?

Also, I would recommend not using os.walk twice, but instead determining the new name and renaming in one go.
Much more efficient that way.

As for removing non-alphanumeric characters, you might want to take a look at re.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1120
Joined: Thu Feb 07, 2013 3:42 pm

Re: Remove spaces and convert capitals to lower case

Postby choptrees » Wed Apr 02, 2014 9:52 pm

Thanks for the reply...

Also, I would recommend not using os.walk twice, but instead determining the new name and renaming in one go.


How?
choptrees
 
Posts: 5
Joined: Wed Apr 02, 2014 9:23 pm

Re: Remove spaces and convert capitals to lower case

Postby choptrees » Wed Apr 02, 2014 9:54 pm

Updated code:

Code: Select all
import os

inDir = r'M:\WORKSPACE\Test'

for (r,d,f) in os.walk(inDir, topdown=False):
    for fld in d:
        curDir1 = os.path.join(r,fld)
        os.rename(curDir1, curDir1.replace(' ', '_'))
        print curDir1
        print 'Replaced all spaces with underscores'

    for flr in d:
        curDir2 = os.path.join(r,flr)
        os.rename(curDir2, curDir2.lower())
        print curDir2
        print 'Replaced all spaces with underscores'
choptrees
 
Posts: 5
Joined: Wed Apr 02, 2014 9:23 pm

Re: Remove spaces and convert capitals to lower case

Postby stranac » Wed Apr 02, 2014 9:56 pm

choptrees wrote:
Also, I would recommend not using os.walk twice, but instead determining the new name and renaming in one go.

How?

Code: Select all
# get original name
name = name.replace(' ', '_')
name = name.lower()
# rename
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1120
Joined: Thu Feb 07, 2013 3:42 pm

Re: Remove spaces and convert capitals to lower case

Postby stranac » Wed Apr 02, 2014 9:59 pm

The reason your updated code doesn't work is you rename the dirs, but you still work with the old names(stored in d).
Doing the renaming all at once should fix this.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1120
Joined: Thu Feb 07, 2013 3:42 pm

Re: Remove spaces and convert capitals to lower case

Postby choptrees » Wed Apr 02, 2014 10:06 pm

Thanks for your reply. When os.walk is used it sets up the folders and file structure, and when you change the names by adding underscores the os.walk no longer recognises the folder name and returns an error. so listing functions one after teh otehr as you have done doesn't work...
choptrees
 
Posts: 5
Joined: Wed Apr 02, 2014 9:23 pm

Re: Remove spaces and convert capitals to lower case

Postby stranac » Wed Apr 02, 2014 10:16 pm

Oh, right, forgot you needed the old name.
Well, you just need to keep the old name available.
Code: Select all
# get original name
new_name = name.replace(' ', '_')
new_name = new_name.lower()
# rename name to new_name

Of course, you could also chain multiple method calls in one line, but that could get messy.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1120
Joined: Thu Feb 07, 2013 3:42 pm

Re: Remove spaces and convert capitals to lower case

Postby choptrees » Wed Apr 02, 2014 10:25 pm

Thanks for your help. I'll take a look at it.
choptrees
 
Posts: 5
Joined: Wed Apr 02, 2014 9:23 pm


Return to General Coding Help

Who is online

Users browsing this forum: Baidu [Spider] and 3 guests