10 examples of 'python get filename without extension' in Python

Every line of 'python get filename without extension' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
41def get_filename(path, with_ext=True):
42 """Returns file name from given file, with or without extension.
43 It finds last "os.sep" inside string and returns tail.
44 :param path: path with file name
45 :type path: str
46 :param with_ext: return file name with extension or not
47 :type with_ext: bool
48 :return: file name with or without extension
49 :rtype: str
50 """
51
52 # find last separator; prefer os.sep otherwise search for normal slash
53 last_sep = path.rfind(os.sep)
54 if last_sep < 0:
55 last_sep = path.rfind("/")
56
57 new_path = path[last_sep + 1:]
58 if not with_ext and new_path.rfind(".") > 0:
59 new_path = new_path[:new_path.rfind(".")]
60 return new_path
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

18def get_filename(filename: str) -> str:
19 return ntpath.basename(filename).split('.')[0]
37def get_fileNameExt(filename):
38 (fileDir, tempfilename) = os.path.split(filename)
39 (shortname, extension) = os.path.splitext(tempfilename)
40 return shortname, extension
21def getBasename(filename):
22 name = os.path.split(filename)[-1]
23 for extension in EXTENSIONS_NXS:
24 name = name.replace(extension, '')
25 return name
18def filenameWithoutExtension(file_name):
19 if '.' in file_name:
20 separator_index = file_name.rindex('.')
21 base_name = file_name[:separator_index]
22 return base_name
23 else:
24 return file_name
27def get_file_name(file_path):
28 """Returns name of file, basing on file_path.
29 Name is acquired by removing parent directories from file_path and stripping
30 extension.
31 i.e. get_file_name("dir1/dir2/file.py") will return "file"
32 """
33 return os.path.splitext(os.path.basename(file_path))[0]
21def remove_file_extension(filename):
22 return os.path.splitext(filename)[0]
495def stripext (filename):
496 """Return the basename without extension of given filename."""
497 return os.path.splitext(os.path.basename(filename))[0]
253def get_extension_from_filename(name):
254 """Extracts an extension from a filename string.
255
256 returns filename, extension
257 """
258 name = name.strip()
259 ext = ((name.split('\\')[-1]).split('/')[-1]).split('.')
260 if len(ext) > 1 and ext[-1] is not '':
261 nameOnly = '.'.join(name.split('.')[:-1])
262 ext = ext[-1]
263 else:
264 nameOnly = name
265 ext = None
266 return nameOnly, ext
190def _filename(obj, filename, folder, extension):
191 map_, username = obj.map.name, obj.user.username
192 return f"uploads/{folder}/{map_[0]}/{username[0]}/{map_}_{username}.{extension}"

Related snippets