How to use the pathvalidate.sanitize_filename function in pathvalidate

To help you get started, we’ve selected a few pathvalidate examples, based on popular ways it is used in public projects.

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

github thombashi / pathvalidate / test / test_filename.py View on Github external
def test_normal_null_values(self, value, expected):
        assert sanitize_filename(value) == expected
github thombashi / pathvalidate / test / test_filename.py View on Github external
def test_normal_max_len(self, value, max_len, expected):
        filename = sanitize_filename(value, max_len=max_len)
        assert len(filename) == expected
        assert is_valid_filename(filename, max_len=max_len)
github thombashi / pathvalidate / test / test_filename.py View on Github external
def test_normal_space_or_period_at_tail(self, platform, value, expected):
        filename = sanitize_filename(value, platform=platform)
        assert filename == expected
        assert is_valid_filename(filename, platform=platform)
github thombashi / pathvalidate / test / test_filename.py View on Github external
def test_exception_type(self, value, expected):
        with pytest.raises(expected):
            sanitize_filename(value)
        assert not is_valid_filename(value)
github ip-tools / uspto-opendata-python / uspto / util / common.py View on Github external
def get_document_path(directory, name, format, source=None):
    if source:
        source = source.lower() + '.'
    filename = pathvalidate.sanitize_filename('{name}.{source}{suffix}'.format(
        name=name.upper(), source=source.lower(), suffix=format.lower()))
    filepath = os.path.join(directory, filename)
    return filepath
github bschollnick / QuickBBS / quickbbs / frontend / cached_exists.py View on Github external
dirpath (string): The path to the files to be sanitized

                allow_rename (boolean): Allow the renaming of the files to
                    conform to a self.sanitize_plat (default-Windows)
                    platform

            .. code-block:
                # Boolean Tests
        """

        refresh = False
        dirpath = os.path.normpath(dirpath.title().strip())
        for filename in self.scanned_paths[dirpath]:
            if not is_valid_filename(filename, platform=self.sanitize_plat):
                new_filename = sanitize_filename(filename,
                                                 platform=self.sanitize_plat)
                print("Invalid Filename: %s --> %s" % (filename, new_filename))
                if allow_rename:
                    refresh = True
                    os.rename(os.path.join(dirpath, filename),
                              os.path.join(dirpath, new_filename))
        if refresh:
            self.clear_path(dirpath)
            self.read_path(dirpath)
github shadowmoose / RedditDownloader / redditdownloader / processing / name_generator.py View on Github external
def _filename(f_name):
	""" Format the given string into an acceptable filename. """
	ret = f_name
	# noinspection PyBroadException
	try:
		ret = pathvalidate.sanitize_filename(ret, '_').strip(' ./\\')
	except Exception:
		ret = '_'
	if len(ret) == 0:
		return '_'
	return ret
github bschollnick / QuickBBS / quickbbs / frontend / utilities.py View on Github external
(lower_filename in configdata["filetypes"]["files_to_ignore"]):
            continue

        elif configdata["filetypes"]["ignore_dotfiles"] and lower_filename.startswith("."):
            continue

        if enable_rename:
            original_filename = titlecase
            if titlecase != unescaped:
                titlecase = unescaped.title()

            after_filename = multiple_replace(constants.replacements, lower_filename)#, regex)
            if after_filename != lower_filename:
                titlecase = after_filename.title()

            titlecase = sanitize_filename(titlecase)
            if titlecase != original_filename:
                rename_file(os.path.join(fqpn, original_filename),
                            os.path.join(fqpn, titlecase))
                print("rejected - %s" % titlecase)
                loaded = False

            data[titlecase] = {"filename":titlecase,
                               "lower_filename":titlecase.lower(),
                               "path":os.path.join(fqpn, titlecase),
                               'sortname':naturalize(titlecase),
                               'size':entry.stat()[stat.ST_SIZE],
                               'lastmod':entry.stat()[stat.ST_MTIME],
                               'is_dir':entry.is_dir(),#fext == ".dir",
                               'is_file':not entry.is_dir(),#fext != ".dir",
                               'is_archive':ftypes.FILETYPE_DATA[fext]["is_archive"],
                               'is_image':ftypes.FILETYPE_DATA[fext]["is_image"],