10-30-2012, 06:52 PM
|
#17
|
|
LQ Newbie
Registered: Oct 2012
Posts: 1
Rep: 
|
Just what I was looking for!
Thanks for posting the awesome script -- it's exactly what I was looking for!
Quote:
Originally Posted by bushidozen
I just looked over your code, and I saw a couple of errors, so I rewrote it a little:
Code:
#!/usr/bin/python
# Replace upper case with lower case
# Replace spaces with underscores
import os, sys
if len(sys.argv) == 1:
filenames = os.listdir(os.curdir)
else:
filenames = sys.argv[1:]
for filename in filenames:
newfilename = filename.lower()
if ' ' in newfilename:
newfilename = newfilename.replace(' ', '_')
print "Renaming", filename, "to", newfilename, "..."
os.rename(filename, newfilename)
This code will attempt to convert all filenames to lowercase (even those that are already lowercase), which could translate into a lot of wasted cycles (ie. if all of the files in the directory are already lowercase), but it actually runs faster than trying to determine its case as in the previous code (by using the if statement). Enjoy.
|
|
|
|
|