10 examples of 'python extract filename from path' in Python

Every line of 'python extract filename from path' 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
55def get_name_from_path(file_path: str) -> str:
56 return file_path.split(os.sep)[-1]
27def _extract_from_file(path: str) -> str:
28 return _GREP_CMD.format(
29 grep_args="-n",
30 regex=_URL_REGEX,
31 path=f"{quote(path)} /dev/null", # dev null to trick into displaying filename
32 )
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]
154def find_module(module_name, path=None):
155 """ Returns the filename for the specified module. """
156
157 components = module_name.split('.')
158
159 try:
160 # Look up the first component of the module name (of course it could be
161 # the *only* component).
162 f, filename, description = imp.find_module(components[0], path)
163
164 # If the module is in a package then go down each level in the package
165 # hierarchy in turn.
166 if len(components) > 0:
167 for component in components[1:]:
168 f,filename,description = imp.find_module(component, [filename])
169
170 except ImportError:
171 filename = None
172
173 return filename
8def filename_to_module_name(filename):
9 if os.path.dirname(filename).startswith(os.pardir):
10 # Don't try to infer a module name for filenames starting with ../
11 return None
12 return filename.replace(os.sep, ".")
18def get_filename(filename: str) -> str:
19 return ntpath.basename(filename).split('.')[0]
225def path_to_module_format(py_path):
226 """Transform a python file path to module format."""
227 return py_path.replace("/", ".").rstrip(".py")
249def get_module_path(fpath):
250 """Given a module filename, return its full Python name including
251 enclosing packages. (based on existence of ``__init__.py`` files)
252 """
253 if basename(fpath).startswith('__init__.'):
254 pnames = []
255 else:
256 pnames = [splitext(basename(fpath))[0]]
257 path = dirname(abspath(fpath))
258 while isfile(join(path, '__init__.py')):
259 path, pname = split(path)
260 pnames.append(pname)
261 return '.'.join(pnames[::-1])
42def _is_py(path):
43 '''We need to make sure we are writing out Python files.'''
44 return path.split('.')[-1] == 'py'
59def guess_package_name_from_file_name(path):
60 basename = os.path.basename(os.path.dirname(os.path.abspath(path)))
61
62 if re.match(r'^\w+$', basename):
63 return basename
64 else:
65 return 'main'

Related snippets