10 examples of 'python check file extension' in Python

Every line of 'python check file 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
36def _file_is_python(path):
37 # type: (str) -> bool
38 if path.endswith('.py'):
39 return True
40
41 _, extension = os.path.splitext(path)
42 if extension:
43 return False
44 try:
45 with open(path) as might_be_python:
46 line = might_be_python.readline()
47 return line.startswith('#!') and 'python' in line
48 except UnicodeDecodeError:
49 return False
302@staticmethod
303def check_file_extensions(file_name, extensions):
304 """Checks that `file_name` has one of the provided extensions.
305
306 Args:
307 file_name (str): The file name to check.
308 extensions (tuple): A tuple of file extensions to use.
309
310 Raises:
311 ValueError: If `file_name` does not end with one of the extensions.
312 """
313 if file_name is None:
314 return
315 assert isinstance(extensions, tuple), "The 'extensions' must be a tuple."
316 if not file_name.endswith(extensions):
317 raise ValueError("Invalid file extension (%s). Must be one of %s" % extensions)
25def is_python(filepath):
26 return filepath.endswith('.py')
4def file_extension(file):
5 pattern = re.compile(r"(\.\w+$)")
6 match = pattern.search(file)
7 print("Input file: \033[1;32m{}\033[0m".format(file))
8 print("Extension: ", end="")
9 return match.group()
24def file_extension(path): # 文件扩展名
25 return os.path.splitext(path)[1]
8def get_ext(file):
9 filename, file_extension = os.path.splitext(file)
10 if "." in file_extension:
11 param, value = file_extension.split(".",1)
12 return value, filename
422def _file_type(self, ext, check_exists=True):
423 name = join(self._directory, self.base + ext)
424 if not check_exists or exists(name):
425 return name
426 raise NoFileError("Cannot find '{}'.".format(name))
984def is_python(self, path):
985 """Test whether argument path is a Python script."""
986 head, tail = os.path.splitext(path)
987 return tail.lower() in (".py", ".pyw")
572def check_for_known_extensions(filenames):
573 accepted_extensions = {'.tif', '.h5'}
574 appropriate_extension = True
575 for filename in filenames:
576 file_format = os.path.splitext(filename)[-1]
577 if file_format not in accepted_extensions:
578 appropriate_extension = False
579 break
580 return appropriate_extension
69def allow_filetype(filename):
70 ''' what kinds of files are allowed? '''
71
72 return splitext(filename)[-1].lower() in \
73 ['.png','.jpg','.jpeg','.gif','.bmp','.svg']

Related snippets