Class: QgsFileUtils¶
- class qgis.core.QgsFileUtils¶
Bases:
sip.wrapper
Class for file utilities.
New in version 3.0:
Methods
Ensures that a
fileName
ends with an extension from the specifiedfilter
string.Ensures that a
fileName
ends with an extension from the provided list ofextensions
.Returns a list of the extensions contained within a file
filter
string. E.g. afilter
of "GeoTIFF Files (*.tiff *.tif)" would return a list containing "tiff", "tif". The initial '.' is stripped off the extension.Returns the top-most existing folder from
path
.Will check
basepath
in an outward spiral up tomaxClimbs
levels to check iffile
exists.Returns the human size from bytes
Converts a
string
to a safe filename, replacing characters which are not safe for filenames with an '_' character.- addExtensionFromFilter(fileName: str, filter: str) str ¶
Ensures that a
fileName
ends with an extension from the specifiedfilter
string.E.g. a
fileName
of “d:/my_file” with a filter of “GeoTIFF Files (*.tiff *.tif)” will return “d:/my_file.tif”, where asfileName
of “d:/my_file.TIFF” or “d:/my_file.TIF” will be returned unchanged.See also
See also
- Parameters
fileName (str) –
filter (str) –
- Return type
str
- ensureFileNameHasExtension(fileName: str, extensions: Iterable[str]) str ¶
Ensures that a
fileName
ends with an extension from the provided list ofextensions
.E.g. if extensions contains “tif” and “tiff”, then a
fileName
of “d:/my_file” will return “d:/my_file.tif”. AfileName
of “d:/my_file.TIFF” or “d:/my_file.TIF” will be returned unchanged.See also
See also
- Parameters
fileName (str) –
extensions (Iterable[str]) –
- Return type
str
- extensionsFromFilter(filter: str) List[str] ¶
Returns a list of the extensions contained within a file
filter
string. E.g. afilter
of “GeoTIFF Files (*.tiff *.tif)” would return a list containing “tiff”, “tif”. The initial ‘.’ is stripped off the extension.See also
See also
- Parameters
filter (str) –
- Return type
List[str]
- findClosestExistingPath(path: str) str ¶
Returns the top-most existing folder from
path
. E.g. ifpath
is “/home/user/projects/2018/P4343” and “/home/user/projects” exists but no “2018” subfolder exists, then the function will return “/home/user/projects”.New in version 3.2.
- Parameters
path (str) –
- Return type
str
- findFile(file: str, basepath: str = '', maxClimbs: int = 4, searchCeiling: int = 4, currentDir: str = '') List[str] ¶
Will check
basepath
in an outward spiral up tomaxClimbs
levels to check iffile
exists.- Parameters
file (str) – Name or full path of the file to find
basepath (str = '') – current basepath of the file, needed if only the name is specified in file
maxClimbs (int = 4) – limit the number of time the search can move up from the basepath
searchCeiling (int = 4) – limits where in the folder hierarchy the search can be performed, 1 = root/drive, 2 = first folder level, 3 = sub folders ( Unix: /usr/bin, Win: C:/Users/Admin ), etc.
currentDir (str = '') – alternative default directory to override the actual current directory during the search
- Return type
List[str]
- Returns
List of strings of the first matching path in unix format.
New in version 3.12.
- representFileSize(bytes: int) str ¶
Returns the human size from bytes
- Parameters
bytes (int) –
- Return type
str
- stringToSafeFilename(string: str) str ¶
Converts a
string
to a safe filename, replacing characters which are not safe for filenames with an ‘_’ character.Warning
This method strips slashes from the filename, so it is safe to call with file names only, not complete paths.
- Parameters
string (str) –
- Return type
str