1622

How do I get the filename without the extension from a path in Python?

"/path/to/some/file.txt"  →  "file"
Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135
Joan Venge
  • 315,713
  • 212
  • 479
  • 689
  • 112
    I had to scroll pretty far before coming across the clear right answer for modern Python: `from pathlib import Path; print(Path("/path/to/some/file.txt").stem)` >>> `file` – BallpointBen Jan 31 '21 at 04:39

31 Answers31

1731

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.

Ord
  • 5,693
  • 5
  • 28
  • 42
Geo
  • 93,257
  • 117
  • 344
  • 520
  • 29
    If this is a common enough operation, perhaps it should merit it's own official command? Something like os.path.filename(path_to_file) instead of os.path.splitext(os.path.basename(path_to_file))[0] – Fnord Jul 02 '14 at 17:13
  • 28
    What if the filename contains multiple dots? – matteok Dec 05 '14 at 17:42
  • 119
    For anyone wondering the same as matteok, if there are multiple dots, splitext splits at the last one (so `splitext('kitty.jpg.zip')` gives `('kitty.jpg', '.zip')`). – Chuck Jan 22 '15 at 18:15
  • Bears some similarity to [this SO question](http://stackoverflow.com/questions/8384737/python-extract-file-name-from-path-no-matter-what-the-os-path-format) as well. – franklin Jan 16 '16 at 02:40
  • @FNord sorry, not just to bump this, but it's easy, as long as you don't mean to have it standardized in the Python Stdlib: `def filename_without_ext(path_to_file): return os.path.splitext(path_to_file)[0]` – wallabra May 26 '16 at 21:33
  • print (basename("c:\text\test\dir\ddd.tab")) show "ddd.tab" – Kostadin Jan 09 '18 at 08:14
  • 83
    Note that this code returns the complete file *path* (without the extension), not just the file *name*. – Aran-Fey Oct 17 '18 at 07:13
  • 18
    yeah, so you'd have to do `splitext(basename('/some/path/to/file.txt'))[0]` (which i _always_ seem to be doing) – CpILL Nov 04 '19 at 23:51
  • 2
    this isn't the answer, please update this with CplLLs answer since theres no way we are overcoming 1347 votes! arg – Geronimo Aug 05 '20 at 02:38
  • 4
    This is not a correct answer. Use `os.path.splitext(os.path.basename(protocol))[0]` or please refer @gimel – int soumen Mar 05 '21 at 06:09
  • 2
    This is not the answer to the OPs question! – Fred Apr 30 '21 at 12:15
  • apparently, the expretion `os.path.splitext("/path/to/some/file.txt.zip/?aaa=111")[0]` returns `'/path/to/some/file.txt.zip/?aaa=111'`. meaning it only splits on the last dot if such exists after the last `/` (slash). – Yuval Zilber Jul 08 '21 at 10:23
  • This a wrong answer to the question. You say it prints `/path/to/some/file` but author explicitly said they want just `file`, not `/path/to/some/file`. – KulaGGin Apr 14 '23 at 17:02
1249

Use .stem from pathlib in Python 3.4+

from pathlib import Path

Path('/root/dir/sub/file.ext').stem

will return

'file'

Note that if your file has multiple extensions .stem will only remove the last extension. For example, Path('file.tar.gz').stem will return 'file.tar'.

mxdbld
  • 16,747
  • 5
  • 34
  • 37
  • 102
    This is the recommended way since python 3. – Miladiouss Nov 15 '19 at 02:21
  • 5
    Note that, like `os.path` solutions, this will only strip one extension (or `suffix`, as `pathlib` calls it). `Path('a.b.c').stem == 'a.b'` – BallpointBen Mar 18 '20 at 04:26
  • 2
    @BallpointBen what is the optimal way of stripping multiple suffixes? Surely there must be a better way than `Path(Path('a.b.c').stem).stem` – hoan May 13 '20 at 13:53
  • 8
    @hoan I think repeatedly calling `.with_suffix('')` is the way to go. You'd probably want to loop until `p.suffix == ''`. – BallpointBen May 13 '20 at 15:00
  • 4
    It will not work for files with complex extensions: `pathlib.Path('backup.tar.gz').stem` -> `'backup.tar` but expected `backup` – pymen Jun 15 '20 at 11:11
  • 2
    If the file name with extension is desired, use `name` instead of `stem`. – flow2k Oct 27 '20 at 17:45
  • 8
    @pymen it depends on what you define as "extension". How about `Fantastic Mr.Fox.mp4`? – spectras May 06 '21 at 21:59
  • @Miladiouss recommended by whom? – Duncan MacIntyre Jun 26 '21 at 06:02
  • @DuncanMacIntyre PEP-428: "using a dedicated class allows to enable desirable behaviours by default". But of course; it depends on your task and context. – Seth Sep 03 '21 at 08:45
  • Slightly different question from OP's: How do we keep the directory information just lose the extension, i.e. to return '/root/dir/sub/file'? – sh37211 Sep 11 '21 at 05:56
  • @sh37211 Path(string).parent will do that. The pathlib library in general has so many common and easy to use methods for stuff like this. https://docs.python.org/3/library/pathlib.html – Nevermore Jan 01 '22 at 16:55
  • @Nevermore No, that doesn't work for me, because it loses the filename portion: Path('/root/dir/sub/file.ext').parent returns '/root/dir/sub', not '/root/dir/sub/file' as I requested. Seems however that one answer to my question would be to use Path(string).parent / Path(string).stem to combine the two. – sh37211 Jan 13 '22 at 03:42
  • 3
    @sh37211 Oh, I completely misunderstood your original question. Very easy solution: `Path(string).with_suffix('')` should do the trick. with_suffix() changes the suffix of a Path object to whatever string is passed in, in this case we pass in an empty string to effectively remove it. – Nevermore Jan 13 '22 at 07:46
  • 1
    This answer should be updated with @Nevermore's solution. – xilopaint Jun 13 '22 at 22:52
  • @pymen How about this for handling multiple extensions https://stackoverflow.com/a/75504878/5694144 – John Cole Feb 20 '23 at 03:01
760

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.

Alan W. Smith
  • 24,647
  • 4
  • 70
  • 96
gimel
  • 83,368
  • 10
  • 76
  • 104
271
>>> print(os.path.splitext(os.path.basename("/path/to/file/hemanth.txt"))[0])
hemanth
naqushab
  • 754
  • 1
  • 8
  • 24
hemanth.hm
  • 4,449
  • 2
  • 22
  • 10
  • 9
    +1 for this. 3 exact same answers, but this is the most direct one. You just could have used `\`` for showing the code, and "/somepath/hermanth.txt" as a path instance. – cregox May 21 '10 at 20:57
  • 2
    @hemanth.hm Note that in this statement you provided, `os.path.basename` is not necessary. `os.path.basename` should be only used to get the file name from the file path. – arrt_ Jan 25 '18 at 12:53
115

In Python 3.4+ you can use the pathlib solution

from pathlib import Path

print(Path(your_path).resolve().stem)
Boris Verkhovskiy
  • 14,854
  • 11
  • 100
  • 103
Morgoth
  • 4,935
  • 8
  • 40
  • 66
  • 8
    Why do you [`resolve()`](https://docs.python.org/library/pathlib.html#pathlib.Path.resolve) the path? Is it really possible to get a path to a file and not have the filename be a part of the path without that? This means that if you're give a path to symlink, you'll return the filename (without the extension) of the file the symlink points to. – Boris Verkhovskiy Oct 11 '19 at 15:43
  • 3
    One possible reason to use `resolve()` is to help deal with the multiple dots problem. The answer below about using the index will not work if the path is './foo.tar.gz' – William Allcock Feb 12 '20 at 22:51
92

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")
>>> p.with_suffix('')
WindowsPath('/a/b/c')
>>> p.stem
'c'
jjisnow
  • 1,418
  • 14
  • 5
  • 12
    This is the best python 3 solution for the generic case of removing the extension from a full path. Using stem also removes the parent path. In case you are expecting a double extension (such as bla.tar.gz) then you can even use it twice: p.with_suffix('').with_suffix(''). – Eelco van Vliet Feb 26 '20 at 12:37
40

As noted by @IceAdor in a comment to @user2902201's solution, rsplit is the simplest solution robust to multiple periods (by limiting the number of splits to maxsplit of just 1 (from the end of the string)).

Here it is spelt out:

file = 'my.report.txt'
print file.rsplit('.', maxsplit=1)[0]

my.report

mirekphd
  • 4,799
  • 3
  • 38
  • 59
dlink
  • 1,489
  • 17
  • 22
  • This approach fails if files without extensions are located in directories with dot(s) in the name, e.g. `./readme`. – Wolf Feb 22 '23 at 09:21
32

If you want to keep the path to the file and just remove the extension

>>> file = '/root/dir/sub.exten/file.data.1.2.dat'
>>> print ('.').join(file.split('.')[:-1])
/root/dir/sub.exten/file.data.1.2
  • 22
    If you want to split on the last period, use rsplit: `'/root/dir/sub.exten/file.data.1.2.dat'.rsplit('.', 1)` – IceArdor Dec 04 '14 at 22:32
32

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
  • 1
    index_of_dot = file_name.index('.') This will be done after getting the basename of the file so that it wont split at .env – Dheeraj Chakravarthi Oct 05 '16 at 04:22
  • 2
    Important point, as a series of extensions like this is common. `.tar.gz` `.tar.bz` `.tar.7z` –  Oct 18 '17 at 12:23
  • 3
    Note that `'haystack'.index('needle')` throws a ValueError exception if the needle (in above case the dot, `.`) is not found in haystack. Files without any extension exist too. – Czechnology Mar 27 '18 at 07:54
  • to solve that problem, use a try-catch, or use `str.find()` and check for -1. if there's no dot, then just return `file_name` – starwarswii Jun 15 '21 at 17:52
27

Answers using Pathlib for Several Scenarios

Using Pathlib, it is trivial to get the filename when there is just one extension (or none), but it can be awkward to handle the general case of multiple extensions.

Zero or One extension

from pathlib import Path

pth = Path('./thefile.tar')

fn = pth.stem

print(fn)      # thefile


# Explanation:
# the `stem` attribute returns only the base filename, stripping
# any leading path if present, and strips the extension after
# the last `.`, if present.


# Further tests

eg_paths = ['thefile',
            'thefile.tar',
            './thefile',
            './thefile.tar',
            '../../thefile.tar',
            '.././thefile.tar',
            'rel/pa.th/to/thefile',
            '/abs/path/to/thefile.tar']

for p in eg_paths:
    print(Path(p).stem)  # prints thefile every time

Two or fewer extensions

from pathlib import Path

pth = Path('./thefile.tar.gz')

fn = pth.with_suffix('').stem

print(fn)      # thefile


# Explanation:
# Using the `.with_suffix('')` trick returns a Path object after
# stripping one extension, and then we can simply use `.stem`.


# Further tests

eg_paths += ['./thefile.tar.gz',
             '/abs/pa.th/to/thefile.tar.gz']

for p in eg_paths:
    print(Path(p).with_suffix('').stem)  # prints thefile every time

Any number of extensions (0, 1, or more)

from pathlib import Path

pth = Path('./thefile.tar.gz.bz.7zip')

fn = pth.name
if len(pth.suffixes) > 0:
    s = pth.suffixes[0]
    fn = fn.rsplit(s)[0]

# or, equivalently

fn = pth.name
for s in pth.suffixes:
    fn = fn.rsplit(s)[0]
    break

# or simply run the full loop

fn = pth.name
for _ in pth.suffixes:
    fn = fn.rsplit('.')[0]

# In any case:

print(fn)     # thefile


# Explanation
#
# pth.name     -> 'thefile.tar.gz.bz.7zip'
# pth.suffixes -> ['.tar', '.gz', '.bz', '.7zip']
#
# If there may be more than two extensions, we can test for
# that case with an if statement, or simply attempt the loop
# and break after rsplitting on the first extension instance.
# Alternatively, we may even run the full loop and strip one 
# extension with every pass.


# Further tests

eg_paths += ['./thefile.tar.gz.bz.7zip',
             '/abs/pa.th/to/thefile.tar.gz.bz.7zip']

for p in eg_paths:
    pth = Path(p)
    fn = pth.name
    for s in pth.suffixes:
        fn = fn.rsplit(s)[0]
        break

    print(fn)  # prints thefile every time

Special case in which the first extension is known

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
SpinUp __ A Davis
  • 5,016
  • 1
  • 30
  • 25
25
import os
filename, file_extension =os.path.splitext(os.path.basename('/d1/d2/example.cs'))
  • filename is 'example'

  • file_extension is '.cs'

'

MEdwin
  • 2,940
  • 1
  • 14
  • 27
Antonio Ramasco
  • 431
  • 5
  • 3
21

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'
ScottMcC
  • 4,094
  • 1
  • 27
  • 35
21

But even when I import os, I am not able to call it path.basename. Is it possible to call it as directly as basename?

import os, and then use os.path.basename

importing os doesn't mean you can use os.foo without referring to os.

Devin Jeanpierre
  • 92,913
  • 4
  • 55
  • 79
14

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
Alan W. Smith
  • 24,647
  • 4
  • 70
  • 96
7

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'
6
import os
path = "a/b/c/abc.txt"
print os.path.splitext(os.path.basename(path))[0]
Morten Jensen
  • 5,818
  • 3
  • 43
  • 55
user4949344
  • 89
  • 1
  • 1
6

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)
oak
  • 2,898
  • 2
  • 32
  • 65
learncode
  • 1,105
  • 4
  • 18
  • 36
6

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)
Bilal
  • 1,254
  • 13
  • 14
4

On Windows system I used drivername prefix as well, like:

>>> s = 'c:\\temp\\akarmi.txt'
>>> print(os.path.splitext(s)[0])
c:\temp\akarmi

So because I do not need drive letter or directory name, I use:

>>> print(os.path.splitext(os.path.basename(s))[0])
akarmi
Zéiksz
  • 688
  • 1
  • 11
  • 25
3

Improving upon @spinup answer:

fn = pth.name
for s in pth.suffixes:
    fn = fn.rsplit(s)[0]
    break
    
print(fn)      # thefile 

This works for filenames without extension also

M Ganesh
  • 45
  • 1
  • 9
3

I've read the answers, and I notice that there are many good solutions. So, for those who are looking to get either (name or extension), here goes another solution, using the os module, both methods support files with multiple extensions.

import os

def get_file_name(path):
    if not os.path.isdir(path):
        return os.path.splitext(os.path.basename(path))[0].split(".")[0]


def get_file_extension(path):
    extensions = []
    copy_path = path
    while True:
        copy_path, result = os.path.splitext(copy_path)
        if result != '':
            extensions.append(result)
        else:
            break
    extensions.reverse()
    return "".join(extensions)

Note: this solution on windows does not support file names with the "\" character

esteban21
  • 139
  • 9
3

Using pathlib.Path.stem is the right way to go, but here is an ugly solution that is way more efficient than the pathlib based approach.

You have a filepath whose fields are separated by a forward slash /, slashes cannot be present in filenames, so you split the filepath by /, the last field is the filename.

The extension is always the last element of the list created by splitting the filename by dot ., so if you reverse the filename and split by dot once, the reverse of the second element is the file name without extension.

name = path.split('/')[-1][::-1].split('.', 1)[1][::-1]

Performance:

Python 3.9.10 (tags/v3.9.10:f2f3f53, Jan 17 2022, 15:14:21) [MSC v.1929 64 bit (AMD64)]
Type 'copyright', 'credits' or 'license' for more information
IPython 7.28.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: from pathlib import Path

In [2]: file = 'D:/ffmpeg/ffmpeg.exe'

In [3]: Path(file).stem
Out[3]: 'ffmpeg'

In [4]: file.split('/')[-1][::-1].split('.', 1)[1][::-1]
Out[4]: 'ffmpeg'

In [5]: %timeit Path(file).stem
6.15 µs ± 433 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

In [6]: %timeit file.split('/')[-1][::-1].split('.', 1)[1][::-1]
671 ns ± 37.8 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

In [7]:
Ξένη Γήινος
  • 2,181
  • 1
  • 9
  • 35
2

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
Community
  • 1
  • 1
yckart
  • 32,460
  • 9
  • 122
  • 129
2

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.

handle
  • 5,859
  • 3
  • 54
  • 82
2
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
alessandrio
  • 4,282
  • 2
  • 29
  • 40
2

the easiest way to resolve this is to

import ntpath 
print('Base name is ',ntpath.basename('/path/to/the/file/'))

this saves you time and computation cost.

2

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
John Carrell
  • 1,662
  • 3
  • 21
  • 31
1

Assuming you're already using pathlib and Python 3.9 or newer, here's a simple one-line approach that removes all extensions.

>>> from pathlib import Path
>>> pth = Path("/path/to.some/file.foo.bar.txt")
>>> pth.name.removesuffix("".join(pth.suffixes))
'file'
John Cole
  • 153
  • 1
  • 8
0

What about the following?

import pathlib
filename = '/path/to/dir/stem.ext.tar.gz'
pathlib.Path(filename).name[:-len(''.join(pathlib.Path(filename).suffixes))]
# -> 'stem'

or this equivalent?

pathlib.Path(filename).name[:-sum(map(len, pathlib.Path(filename).suffixes))]
wolfrevo
  • 6,651
  • 2
  • 26
  • 38
0

>>>print(os.path.splitext(os.path.basename("/path/to/file/varun.txt"))[0]) varun

Here /path/to/file/varun.txt is the path to file and the output is varun

-1
# use pathlib. the below works with compound filetypes and normal ones
source_file = 'spaces.tar.gz.zip.rar.7z'
source_path = pathlib.Path(source_file)
source_path.name.replace(''.join(source_path.suffixes), '')
>>> 'spaces'

despite the many working implementations described above I added this ^ as it uses pathlib only and works for compound filetypes and normal ones

John---
  • 69
  • 1
  • 4
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 19 '21 at 14:33
  • I need from `"/path/to/some/file.txt"` to get the `/path/to/some/` in a string. How can I achieve that? – just_learning Oct 29 '21 at 10:09