# encoding:UTF-8 # Listing 9-10. Making a Script a Module ''' If you wanted to turn chext.py into a module that could also be used from other modules, you could put this code into a function and separate its use as a script like this: ''' import sys import os def change_ext(directory, old_ext, new_ext): for f in os.listdir(directory): base, ext = os.path.splitext(f) if ext[1:] == old_ext: os.rename(f, "%s.%s" % (base, new_ext)) if __name__ == '__main__': if len(sys.argv) < 4: print("usage: %s directory old_ext new_ext" % sys.argv[0]) sys.exit(1) change_ext(sys.argv[1], sys.argv[2], sys.argv[3]) ''' python chext.py . foo java OR jython chext.py . foo java '''