Quantcast
Channel: How do I get the filename without the extension from a path in Python? - Stack Overflow
Browsing all 63 articles
Browse latest View live

Answer by Antonio Ramasco for How to get the filename without the extension...

import os filename, file_extension = os.path.splitext('/d1/d2/example.cs') filename is '/d1/d2/example' file_extension is '.cs'

View Article



Answer by amc for How to get the filename without the extension from a path...

I think the easiest way is to use .split("/") input= "PATH/TO/FILE/file.txt" file_only = input.split("/")[-1] print(file_only) >>> file.txt You can also do this to extract the last folder:...

View Article

Answer by Alan W. Smith for How to get the filename without the extension...

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...

View Article

Answer by Connor for How to get the filename without the extension from a...

For maximum esoterica, and for a fun oneliner, and to learn a little about itertools: def strip_suffix(filename): """ >>> video.mp4 video >>> video.extra.mp4 video.extra """ return...

View Article

Answer by jjisnow for How to get the filename without the extension from a...

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")...

View Article


Answer by ScottMcC for How to get the filename without the extension from a...

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 =...

View Article

Answer by bold for How to get the filename without the extension from a path...

Using pathlib in Python 3.4+ from pathlib import Path Path('/root/dir/sub/file.ext').stem will return file

View Article

Answer by joseph nkoro for How to get the filename without the extension from...

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.

View Article


Answer by Lycan for How to get the filename without the extension from a path...

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('.')...

View Article


Answer by Morgoth for How to get the filename without the extension from a...

In Python 3.4+ you can use the pathlib solution from pathlib import Path print(Path(your_path).resolve().stem)

View Article

Answer by shivendra singh for How to get the filename without the 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):...

View Article

Answer by dlink for How to get the filename without the extension from a path...

@IceAdor's refers to rsplit in a comment to @user2902201's solution. rsplit is the simplest solution that supports multiple periods. Here it is spelt out: file = 'my.report.txt' print file.rsplit('.',...

View Article

Answer by learncode for How to get the filename without the extension from a...

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 =...

View Article


Answer by handle for How to get the filename without the extension from a...

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 """...

View Article

Answer by Dheeraj Chakravarthi for How to get the filename without the...

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 =...

View Article


Answer by user4949344 for How to get the filename without the extension from...

import os path = "a/b/c/abc.txt" print os.path.splitext(os.path.basename(path))[0]

View Article

Answer by yckart for How to get the filename without the extension from a...

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...

View Article


Answer by user2902201 for How to get the filename without the extension from...

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])...

View Article

Answer by Zéiksz for How to get the filename without the extension from a...

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...

View Article

Answer by hemanth.hm for How to get the filename without the extension from a...

>>> print(os.path.splitext(os.path.basename("hemanth.txt"))[0]) hemanth

View Article
Browsing all 63 articles
Browse latest View live




Latest Images