Welcome, guest | Sign In | My Account | Store | Cart

lowers the case of all the items found in a specified directory

Python, 15 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
import os
import shutil

def lower_names(dir):
    assert(os.path.isdir(dir))
    [os.rename( os.path.join( dir, file ),
            os.path.join( dir, file.lower() ))
                            for file in os.listdir(dir)]
def get_dir():
    input = raw_input('Enter Directory Name: ')
    return input

if __name__ == '__main__':
    dir = get_dir()
    lower_names(dir)

I wrote this because my friend had a directory of html files where the first letter of each file was upper case, and these files were referenced with all lower case file names in the html they contained. I used a lot of the newer features of Python to whip up this script.

1 comment

Noah Spurrier 18 years, 2 months ago  # | flag

Improved version. Here is an improved version that uses generators in Python 2.3. This version will also handle subdirectories and directory names.

#!/usr/bin/env python

import os

top = raw_input('Enter directory name: ')

for root, dirs, files in os.walk(top, topdown=False):
    for filename in files:
        os.rename(os.path.join(root, filename), os.path.join(root, filename.lower()))
    for dirname in dirs:
        os.rename(os.path.join(root, dirname), os.path.join(root, dirname.lower()))