Changing file extension in Python
Asked Answered
D

9

138

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?

Dorset answered 24/5, 2010 at 20:19 Comment(1)
Does this answer your question? How to replace (or strip) an extension from a filename in Python?Lionellionello
I
109

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)
Incipit answered 24/5, 2010 at 20:23 Comment(5)
Can you be more specific, i saw documentation before too but didn't work.Dorset
Use the first function to get the base. Combine it with the new extension and pass the old filename and the new filename to the second function.Incipit
actually, its better to use this method instead for python3: pathlib.path(pathtofile).with_suffix(".mynewext"). The way, suggested with pathlib.path(pathtofile).stem works but will delete the path before the basename.Antic
str(pathlib.path(pathtofile).with_suffix(".mynewext"))Antic
There's no real need for assigning a variable to the original extension. I find this is more straightforward: os.rename(filename, os.path.splitext(filename)[0] + '.aln')Salad
D
139

An elegant way using pathlib.Path:

from pathlib import Path
p = Path('mysequence.fasta')
p.rename(p.with_suffix('.aln'))
Donavon answered 28/11, 2017 at 9:42 Comment(4)
Although the OP didn't ask to perform a rename, it was in the tags, and if you're going to perform a rename and if it's possible the input might have a path and not just a filename, this technique is the right one.Perineurium
Regarding .with_suffix(), the properties .suffix and .suffixes should have setters.Impudence
Oddly enough, it seems like .suffix is read-only, at least as of 3.9.5.Ibnsaud
@Ibnsaud Paths are immutable docs.python.org/3/library/pathlib.html#general-propertiesDonavon
I
109

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)
Incipit answered 24/5, 2010 at 20:23 Comment(5)
Can you be more specific, i saw documentation before too but didn't work.Dorset
Use the first function to get the base. Combine it with the new extension and pass the old filename and the new filename to the second function.Incipit
actually, its better to use this method instead for python3: pathlib.path(pathtofile).with_suffix(".mynewext"). The way, suggested with pathlib.path(pathtofile).stem works but will delete the path before the basename.Antic
str(pathlib.path(pathtofile).with_suffix(".mynewext"))Antic
There's no real need for assigning a variable to the original extension. I find this is more straightforward: os.rename(filename, os.path.splitext(filename)[0] + '.aln')Salad
M
86
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

Martinmas answered 9/9, 2011 at 14:14 Comment(2)
I like this answer more because it provides an example and not just cites the methods needed to accomplish the task. Thanks @MartinmasUneven
base, _ = os.path.splitext(thisFile) is more idiomatic.Criollo
D
28

Starting from Python 3.4 there's pathlib built-in library. So the code could be something like:

from pathlib import Path

filename = "mysequence.fasta"
new_filename = Path(filename).stem + ".aln"

https://docs.python.org/3.4/library/pathlib.html#pathlib.PurePath.stem

I love pathlib :)

Dermatoglyphics answered 10/2, 2017 at 0:29 Comment(4)
This is even better with python 3.6 string interpolation syntax ( python.org/dev/peps/pep-0498 ) new_filename = f"{Path(filename).stem}.aln" 😎😎😎Backchat
Be careful - stem also strips the path if one is present. If you wanted to rename the file and if a path was supplied (which admittedly it wasn't in the question), this technique would fail.Perineurium
Also, the result is a string, no longer a pathlib Path. p.parent / (p.stem + '.aln') will give you a new Path.Elaterite
Warning: Path(filename).stem drops the directory (prefix) part of the filename.Poultice
D
22

Use this:

os.path.splitext("name.fasta")[0]+".aln"

And here is how the above works:

The splitext method separates the name from the extension creating a tuple:

os.path.splitext("name.fasta")

the created tuple now contains the strings "name" and "fasta". Then you need to access only the string "name" which is the first element of the tuple:

os.path.splitext("name.fasta")[0]

And then you want to add a new extension to that name:

os.path.splitext("name.fasta")[0]+".aln"
Darbie answered 11/2, 2015 at 15:2 Comment(0)
S
11

As AnaPana mentioned pathlib is more new and easier in python 3.4 and there is new with_suffix method that can handle this problem easily:

from pathlib import Path
new_filename = Path(mysequence.fasta).with_suffix('.aln')
Sacred answered 13/5, 2020 at 10:23 Comment(0)
F
3

Using pathlib and preserving full path:

from pathlib import Path
p = Path('/User/my/path')
new_p = Path(p.parent.as_posix() + '/' + p.stem + '.aln')
Frederiksberg answered 20/8, 2018 at 18:13 Comment(1)
to make it just a little bit simpler new_p = Path(p.with_suffix('').as_posix() + '.aln')Nashoma
D
3

Sadly, I experienced a case of multiple dots on file name that splittext does not worked well... my work around:

file = r'C:\Docs\file.2020.1.1.xls'
ext = '.'+ os.path.realpath(file).split('.')[-1:][0]
filefinal = file.replace(ext,'')
filefinal = file + '.zip'
os.rename(file ,filefinal)
Disfeature answered 4/8, 2020 at 13:16 Comment(0)
F
1
>> file = r'C:\Docs\file.2020.1.1.xls'
>> ext = '.'+ os.path.realpath(file).split('.')[-1:][0]
>> filefinal = file.replace(ext,'.zip')
>> os.rename(file ,filefinal) 

Bad logic for repeating extension, sample: 'C:\Docs\.xls_aaa.xls.xls'

Faludi answered 10/8, 2020 at 23:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.