How can I replace (or strip) an extension from a filename in Python?
Try os.path.splitext it should do what you want.
import os
print os.path.splitext('/home/user/somefile.txt')[0]+'.jpg' # /home/user/somefile.jpg
os.path.splitext('/home/user/somefile.txt') # returns ('/home/user/somefile', '.txt')
How do I get the filename without the extension from a path in Python?
Getting the name of the file without the extension:
import os
print(os.path.splitext("/path/to/some/file.txt")[0])
Prints:
/path/to/some/file
Documentation for os.path.splitext
.
Important Note: If the filename has multiple dots, only the extension after the last one is removed. For example:
import os
print(os.path.splitext("/path/to/some/file.txt.zip.asc")[0])
Prints:
/path/to/some/file.txt.zip
See other answers below if you need to handle that case.
Strip known extension from filename
I would recomment using the OS library.
name, ext = os.path.splitext(path)
Changing file extension in Python
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)
How can I strip the file extension from a list full of filenames?
You can actually do this in one line with a list comprehension:
lst = [os.path.splitext(x)[0] for x in accounts]
But if you want/need a for-loop, the equivalent code would be:
lst = []
for x in accounts:
lst.append(os.path.splitext(x)[0])
Notice too that I kept the os.path.splitext(x)[0]
part. This is the safest way in Python to remove the extension from a filename. There is no function in the os.path
module dedicated to this task and handcrafting a solution with str.split
or something would be error prone.
Extracting extension from filename in Python
Use os.path.splitext
:
>>> import os
>>> filename, file_extension = os.path.splitext('/path/to/somefile.ext')
>>> filename
'/path/to/somefile'
>>> file_extension
'.ext'
Unlike most manual string-splitting attempts, os.path.splitext
will correctly treat /a/b.c/d
as having no extension instead of having extension .c/d
, and it will treat .bashrc
as having no extension instead of having extension .bashrc
:
>>> os.path.splitext('/a/b.c/d')
('/a/b.c/d', '')
>>> os.path.splitext('.bashrc')
('.bashrc', '')
How to change the files extension using python
To begin with , you can store your mappings in a dictionary, then when you are iterating over your folder, and you find the extension, just use the mapping to make the new file name and save it.
import os
folder ="C:/Users/TestFolder"
#Dictionary for extension mappings
rename_dict = {'txt': 'jpg', 'doc': 'mp3', 'pdf': 'mp4'}
for filename in os.listdir(folder):
#Get the extension and remove . from it
base_file, ext = os.path.splitext(filename)
ext = ext.replace('.','')
#If you find the extension to rename
if ext in rename_dict:
#Create the new file name
new_ext = rename_dict[ext]
new_file = base_file + '.' + new_ext
#Create the full old and new path
old_path = os.path.join(folder, filename)
new_path = os.path.join(folder, new_file)
#Rename the file
os.rename(old_path, new_path)
How to *not* change the file extension while changing a filename?
You can use pathlib.Path objects. It has name
and suffix
attributes, and a rename method:
import re
from pathlib import Path
for file in Path(r'C:\tmp').glob('*'):
if not file.is_file():
continue
new_name = re.sub('\d','', file.stem) + file.suffix
file.rename(file.parent/new_name)
The parent
attribute gives the folder the file belongs to and the is_file
method is used to check that we are handing a regular file (and not a folder). New Path objects are created easily with the /
operator (full new filepath is file.parent / new_name
).
The re.sub()
is used to replace the numbers (\d
means a digit) in the old filename stem
part.
remove extension in list python
Remove the extension in a generator expression when you get the filenames with os.path.splitext
. Change:
f.extend(filenames)
to:
f.extend(os.path.splitext(name)[0] for name in filenames)
Update: It looks like you're abusing os.walk
to do file from directory separation (since you break
in the first loop, so you're not actually walking a tree at all), when there are more direct ways to do this. You can replace all of this:
f = []
b = os.walk('C:\\Users\\username\\Desktop\\FOLDER')
for(dirpath, dirnames, filenames) in b:
f.extend(filenames)
break
on Python 3.5+ (or any version of Python after installing the third party scandir
package using scandir.scandir
instead of os.scandir
), with the more succinct and somewhat faster:
f = [os.path.splitext(e.name)[0] for e in os.scandir(r'C:\Users\username\Desktop\FOLDER')
if e.is_file()]
which describes what you really want, and by using os.scandir
, guarantees you don't have to individually stat
each entry in the directory listing to check if it's a file.
If you're on older Python, and can't use the scandir
package, at the cost of a stat
(which os.walk
would pay anyway on older Python), you can do:
f = [os.path.splitext(x)[0] for x in os.listdir(r'C:\Users\username\Desktop\FOLDER')
if os.path.isfile(os.path.join(r'C:\Users\username\Desktop\FOLDER', x))]
Related Topics
How to Tell a Python Script to Use a Particular Version
How to Get Stable Results with Tensorflow, Setting Random Seed
Validation of a Password - Python
How to Combine Python Asyncio with Threads
Python Pack() and Grid() Methods Together
How to Fix "Importerror: Dll Load Failed" While Importing Win32Api
How to Find Unused Functions in Python Code
Factorize a Column of Strings in Pandas
Simple Animation Using Tkinter
Matplotlib Connect Scatterplot Points with Line - Python
How to Extract Info Within a #Shadow-Root (Open) Using Selenium Python
Expand the Line with Specified Width in Data Unit