rename – Changing file extension in Python

rename – Changing file extension in Python

An elegant way using pathlib.Path:

from pathlib import Path
p = Path(mysequence.fasta)
p.rename(p.with_suffix(.aln))

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)

rename – Changing file extension in Python

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

Leave a Reply

Your email address will not be published. Required fields are marked *