10 examples of 'python basename without extension' in Python

Every line of 'python basename 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

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
417def basename(self, path):
418 """ Return the filename part of the given `path`. """
419 if path == ROOT:
420 return ''
421 path = self.normalize(path)
422 return path.rsplit(PATH_SEPARATOR, 1)[1]
12def basename(pathname):
13 return pathname.basename(pathname)
44def basename(p):
45 return os.path.splitext(p)[0]
18def get_filename(filename: str) -> str:
19 return ntpath.basename(filename).split('.')[0]
34def basename(s):
35 if '.tml' in s:
36 s = os.path.basename(s[0:s.index(".tml")])
37 if 'TE3input' in s:
38 s = os.path.basename(s[0:s.index(".TE3input")])
39 return s
27@overload
28def basename(path: bytes) -> bytes: pass
212def get_name_from_path(path: str) -> str:
213 """
214 Get name from path to the file
215 :param path: path to the file
216 :return: name of the file
217 """
218 return os.path.splitext(os.path.basename(path))[0]

Related snippets