Search code examples
pythonrenamefile-extension

Changing file extension in Python


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?


Solution

  • os.path.splitext(), os.rename()

    for example:

    # renamee is the file getting renamed, pre is the part of file name before extension and ext is current extension
    pre, ext = os.path.splitext(renamee)
    os.rename(renamee, pre + new_extension)