How to get the filename without the extension from a path in Python?
For instance, if I had "/path/to/some/file.txt"
, I would want "file"
.
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.
You can make your own with:
>>> import os
>>> base=os.path.basename('/root/dir/sub/file.ext')
>>> base
'file.ext'
>>> os.path.splitext(base)
('file', '.ext')
>>> os.path.splitext(base)[0]
'file'
Important note: If there is more than one .
in the filename, only the last one is removed. For example:
/root/dir/sub/file.ext.zip -> file.ext
/root/dir/sub/file.ext.tar.gz -> file.ext.tar
See below for other answers that address that.
In Python 3.4+ you can use the pathlib
solution
from pathlib import Path
print(Path(your_path).resolve().stem)
https://docs.python.org/3/library/os.path.html
In python 3 pathlib "The pathlib module offers high-level path objects." so,
>>> from pathlib import Path
>>> p = Path("/a/b/c.txt")
>>> print(p.with_suffix(''))
\a\b\c
>>> print(p.stem)
c
os.path.splitext() won't work if there are multiple dots in the extension.
For example, images.tar.gz
>>> import os
>>> file_path = '/home/dc/images.tar.gz'
>>> file_name = os.path.basename(file_path)
>>> print os.path.splitext(file_name)[0]
images.tar
You can just find the index of the first dot in the basename and then slice the basename to get just the filename without extension.
>>> import os
>>> file_path = '/home/dc/images.tar.gz'
>>> file_name = os.path.basename(file_path)
>>> index_of_dot = file_name.index('.')
>>> file_name_without_extension = file_name[:index_of_dot]
>>> print file_name_without_extension
images
Thought I would throw in a variation to the use of the os.path.splitext without the need to use array indexing.
The function always returns a (root, ext)
pair so it is safe to use:
root, ext = os.path.splitext(path)
Example:
>>> import os
>>> path = 'my_text_file.txt'
>>> root, ext = os.path.splitext(path)
>>> root
'my_text_file'
>>> ext
'.txt'
The other methods don't remove multiple extensions. Some also have problems with filenames that don't have extensions. This snippet deals with both instances and works in both Python 2 and 3. It grabs the basename from the path, splits the value on dots, and returns the first one which is the initial part of the filename.
import os
def get_filename_without_extension(file_path):
file_basename = os.path.basename(file_path)
filename_without_extension = file_basename.split('.')[0]
return filename_without_extension
Here's a set of examples to run:
example_paths = [
"FileName",
"./FileName",
"../../FileName",
"FileName.txt",
"./FileName.txt.zip.asc",
"/path/to/some/FileName",
"/path/to/some/FileName.txt",
"/path/to/some/FileName.txt.zip.asc"
]
for example_path in example_paths:
print(get_filename_without_extension(example_path))
In every case, the value printed is:
FileName
As mentioned in the comments of other Pathlib answers, it can be awkward to handle multiple suffixes. Two or fewer suffixes is not so bad to handle with .with_suffix('')
and .stem
.
from pathlib import Path
pth = Path('foo/bar/baz.baz/thefile.tar.gz')
fn = pth.with_suffix('').stem
print(fn) # thefile
If there may be more than two extensions, you may use a loop to handle the general case where there could be 0, 1, or many suffixes.
pth = Path('foo/bar/baz/thefile.tar.gz.bz.7zip')
pth.name # 'thefile.tar.gz.bz.7zip'
pth.suffixes # ['.tar', '.gz', '.bz', '.7zip']
so
fn = pth.name
for s in pth.suffixes:
fn = fn.rsplit(s)[0]
print(fn) # thefile
or
fnp = Path(pth.name)
for _ in fnp.suffixes:
fnp = fnp.with_suffix('')
print(fnp) # thefile
Note here that fnp
is a path, while fn
is a string, which may determine the form of the loop that is preferred.
For instance, if the extension could be .tar
, .tar.gz
, .tar.gz.bz
, etc; you can simply rsplit
the known extension and take the first element:
pth = Path('foo/bar/baz.baz/thefile.tar.gz')
fn = pth.name.rsplit('.tar')[0]
print(fn) # thefile
import os
filename = C:\\Users\\Public\\Videos\\Sample Videos\\wildlife.wmv
This returns the filename
without the extension
(C:\Users\Public\Videos\Sample Videos\wildlife)
temp = os.path.splitext(filename)[0]
Now you can get just the filename
from the temp with
os.path.basename(temp) #this returns just the filename (wildlife)
A multiple extension aware procedure. Works for str
and unicode
paths. Works in Python 2 and 3.
import os
def file_base_name(file_name):
if '.' in file_name:
separator_index = file_name.index('.')
base_name = file_name[:separator_index]
return base_name
else:
return file_name
def path_base_name(path):
file_name = os.path.basename(path)
return file_base_name(file_name)
Behavior:
>>> path_base_name('file')
'file'
>>> path_base_name(u'file')
u'file'
>>> path_base_name('file.txt')
'file'
>>> path_base_name(u'file.txt')
u'file'
>>> path_base_name('file.tar.gz')
'file'
>>> path_base_name('file.a.b.c.d.e.f.g')
'file'
>>> path_base_name('relative/path/file.ext')
'file'
>>> path_base_name('/absolute/path/file.ext')
'file'
>>> path_base_name('Relative\\Windows\\Path\\file.txt')
'file'
>>> path_base_name('C:\\Absolute\\Windows\\Path\\file.txt')
'file'
>>> path_base_name('/path with spaces/file.ext')
'file'
>>> path_base_name('C:\\Windows Path With Spaces\\file.txt')
'file'
>>> path_base_name('some/path/file name with spaces.tar.gz.zip.rar.7z')
'file name with spaces'
Very very very simpely no other modules !!!
import os
p = r"C:\Users\bilal\Documents\face Recognition python\imgs\northon.jpg"
# Get the filename only from the initial file path.
filename = os.path.basename(p)
# Use splitext() to get filename and extension separately.
(file, ext) = os.path.splitext(filename)
# Print outcome.
print("Filename without extension =", file)
print("Extension =", ext)
We could do some simple split
/ pop
magic as seen here (https://stackoverflow.com/a/424006/1250044), to extract the filename (respecting the windows and POSIX differences).
def getFileNameWithoutExtension(path):
return path.split('\\').pop().split('/').pop().rsplit('.', 1)[0]
getFileNameWithoutExtension('/path/to/file-0.0.1.ext')
# => file-0.0.1
getFileNameWithoutExtension('\\path\\to\\file-0.0.1.ext')
# => file-0.0.1
For convenience, a simple function wrapping the two methods from os.path
:
def filename(path):
"""Return file name without extension from path.
See https://docs.python.org/3/library/os.path.html
"""
import os.path
b = os.path.split(path)[1] # path, *filename*
f = os.path.splitext(b)[0] # *file*, ext
#print(path, b, f)
return f
Tested with Python 3.5.
I didn't look very hard but I didn't see anyone who used regex for this problem.
I interpreted the question as "given a path, return the basename without the extension."
e.g.
"path/to/file.json"
=> "file"
"path/to/my.file.json"
=> "my.file"
In Python 2.7, where we still live without pathlib
...
def get_file_name_prefix(file_path):
basename = os.path.basename(file_path)
file_name_prefix_match = re.compile(r"^(?P<file_name_pre fix>.*)\..*$").match(basename)
if file_name_prefix_match is None:
return file_name
else:
return file_name_prefix_match.group("file_name_prefix")
get_file_name_prefix("path/to/file.json")
>> file
get_file_name_prefix("path/to/my.file.json")
>> my.file
get_file_name_prefix("path/to/no_extension")
>> no_extension
import os
list = []
def getFileName( path ):
for file in os.listdir(path):
#print file
try:
base=os.path.basename(file)
splitbase=os.path.splitext(base)
ext = os.path.splitext(base)[1]
if(ext):
list.append(base)
else:
newpath = path+"/"+file
#print path
getFileName(newpath)
except:
pass
return list
getFileName("/home/weexcel-java3/Desktop/backup")
print list
from pathlib import Path; print(Path("/path/to/some/file.txt").stem)
>>>file
– BallpointBen