#! /usr/bin/env python # from __future__ import division # / operator performs float rather than int division import os, sys, string, cp, re def main(): """ %(filename)s Make filenames unix safe. Replaces all whitespace and non-alphanumeric charaters with a char specified in c. {{k check - print old name and new name with out making changes}} {{c replace with this character _}} {{h Prints documentation.}} {{v verbosity 1}} {{version print version info and exit}} %(version)s """ debug = 0 # copy from here... docstringdict = {'filename':os.path.split(sys.argv[0])[-1], 'version':'$Id: unixname.py,v 1.1 2004/10/10 02:53:10 nghoffma Exp $'} optlist, formattedDocString, formattedSummary = cp.optStringParser(main.__doc__ % docstringdict, optWidth=15, lineWidth=60, valOffset = 4, putVals=1) dict = cp.commandparser(options=optlist, usage='Options:\n' + formattedSummary, debug = debug, exitWithUsage=1) if dict.status('version'): sys.exit( 'Version: %(version)s\n' % docstringdict ) #...to here if dict.status('h'): print formattedDocString sys.exit( 0 ) v = dict.value('v') check = dict.status('k') replchar = dict.value('c') #read from a list of fasta files file_list = dict.value('infile_list', 'in') removeCrapRexp = re.compile(r'[\W\s]+') for file in file_list: name, ext = os.path.splitext(file) newname = removeCrapRexp.sub(replchar, name) + ext if v: print '%s --> %s' % (file, newname) if check: print 'skipping...' continue os.rename(file, newname) if __name__ == '__main__': main()