Changing file extension in Python

MysticCodes picture MysticCodes · May 24, 2010 · Viewed 109.5k times · Source

Suppose from index.py with CGI, I have post file foo.fasta to display file. I want to change foo.fasta's file extension to be foo.aln in display file. How can I do it?

Answer

FryDay picture FryDay · Sep 9, 2011
import os
thisFile = "mysequence.fasta"
base = os.path.splitext(thisFile)[0]
os.rename(thisFile, base + ".aln")

Where thisFile = the absolute path of the file you are changing