QFileInfo¶
Synopsis¶
Functions¶
def
__eq__
(fileinfo)def
__ne__
(fileinfo)def
__reduce__
()def
absoluteDir
()def
absoluteFilePath
()def
absolutePath
()def
baseName
()def
birthTime
()def
bundleName
()def
caching
()def
canonicalFilePath
()def
canonicalPath
()def
completeBaseName
()def
completeSuffix
()def
created
()def
dir
()def
exists
()def
fileName
()def
filePath
()def
group
()def
groupId
()def
isAbsolute
()def
isBundle
()def
isDir
()def
isExecutable
()def
isFile
()def
isHidden
()def
isJunction
()def
isNativePath
()def
isReadable
()def
isRelative
()def
isRoot
()def
isShortcut
()def
isSymLink
()def
isSymbolicLink
()def
isWritable
()def
lastModified
()def
lastRead
()def
makeAbsolute
()def
metadataChangeTime
()def
owner
()def
ownerId
()def
path
()def
readLink
()def
refresh
()def
setCaching
(on)def
setFile
(dir, file)def
setFile
(file)def
setFile
(file)def
size
()def
suffix
()def
swap
(other)def
symLinkTarget
()
Static functions¶
def
exists
(file)
Detailed Description¶
QFileInfo
provides information about a file’s name and position (path) in the file system, its access rights and whether it is a directory or symbolic link, etc. The file’s size and last modified/read times are also available.QFileInfo
can also be used to obtain information about a Qt resource .A
QFileInfo
can point to a file with either a relative or an absolute file path. Absolute file paths begin with the directory separator “/” (or with a drive specification on Windows). Relative file names begin with a directory name or a file name and specify a path relative to the current working directory. An example of an absolute path is the string “/tmp/quartz”. A relative path might look like “src/fatlib”. You can use the functionisRelative()
to check whether aQFileInfo
is using a relative or an absolute file path. You can call the functionmakeAbsolute()
to convert a relativeQFileInfo
‘s path to an absolute path.Note
Paths starting with a colon (: ) are always considered absolute, as they denote a
QResource
.The file that the
QFileInfo
works on is set in the constructor or later withsetFile()
. Useexists()
to see if the file exists andsize()
to get its size.The file’s type is obtained with
isFile()
,isDir()
andisSymLink()
. ThesymLinkTarget()
function provides the name of the file the symlink points to.On Unix (including macOS and iOS), the property getter functions in this class return the properties such as times and size of the target file, not the symlink, because Unix handles symlinks transparently. Opening a symlink using
QFile
effectively opens the link’s target. For example:info1 = QFileInfo("/home/bob/bin/untabify") info1.isSymLink() # returns true info1.absoluteFilePath() # returns "/home/bob/bin/untabify" info1.size() # returns 56201 info1.symLinkTarget() # returns "/opt/pretty++/bin/untabify" info2 = QFileInfo(info1.symLinkTarget()) info1.isSymLink() # returns False info1.absoluteFilePath() # returns "/opt/pretty++/bin/untabify" info1.size() # returns 56201On Windows, shortcuts (
.lnk
files) are currently treated as symlinks. As on Unix systems, the property getters return the size of the targeted file, not the.lnk
file itself. This behavior is deprecated and will likely be removed in a future version of Qt, after which.lnk
files will be treated as regular files.info1 = QFileInfo("C:\\Documents and Settings\\Bob\\untabify.lnk") info1.isSymLink() # returns True info1.absoluteFilePath() # returns "C:/Documents and Settings/Bob/untabify.lnk" info1.size() # returns 743 info1.symLinkTarget() # returns "C:/Pretty++/untabify" info2 = QFileInfo(info1.symLinkTarget()) info1.isSymLink() # returns False info1.absoluteFilePath() # returns "C:/Pretty++/untabify" info1.size() # returns 63942Elements of the file’s name can be extracted with
path()
andfileName()
. ThefileName()
‘s parts can be extracted withbaseName()
,suffix()
orcompleteSuffix()
.QFileInfo
objects to directories created by Qt classes will not have a trailing file separator. If you wish to use trailing separators in your own file info objects, just append one to the file name given to the constructors orsetFile()
.The file’s dates are returned by
birthTime()
,lastModified()
,lastRead()
andfileTime()
. Information about the file’s access permissions is obtained withisReadable()
,isWritable()
andisExecutable()
. The file’s ownership is available fromowner()
,ownerId()
,group()
andgroupId()
. You can examine a file’s permissions and ownership in a single statement using thepermission()
function.Note
On NTFS file systems, ownership and permissions checking is disabled by default for performance reasons. To enable it, include the following line:
extern Q_CORE_EXPORT int qt_ntfs_permission_lookup;Permission checking is then turned on and off by incrementing and decrementing
qt_ntfs_permission_lookup
by 1.qt_ntfs_permission_lookup += 1 // turn checking on qt_ntfs_permission_lookup += 1 // turn it off again
Performance Issues¶
Some of
QFileInfo
‘s functions query the file system, but for performance reasons, some functions only operate on the file name itself. For example: To return the absolute path of a relative file name,absolutePath()
has to query the file system. Thepath()
function, however, can work on the file name directly, and so it is faster.Note
To speed up performance,
QFileInfo
caches information about the file.Because files can be changed by other users or programs, or even by other parts of the same program, there is a function that refreshes the file information:
refresh()
. If you want to switch off aQFileInfo
‘s caching and force it to access the file system every time you request information from it callsetCaching
(false).
- class PySide2.QtCore.QFileInfo¶
PySide2.QtCore.QFileInfo(dir, file)
PySide2.QtCore.QFileInfo(file)
PySide2.QtCore.QFileInfo(fileinfo)
PySide2.QtCore.QFileInfo(file)
- param dir:
- param fileinfo:
- param file:
str
Constructs an empty
QFileInfo
object.Note that an empty
QFileInfo
object contain no file reference.See also
Constructs a new
QFileInfo
that gives information about the givenfile
in the directorydir
.If
dir
has a relative path, theQFileInfo
will also have a relative path.If
file
is an absolute path, then the directory specified bydir
will be disregarded.See also
- PySide2.QtCore.QFileInfo.__reduce__()¶
- Return type:
object
- PySide2.QtCore.QFileInfo.absoluteDir()¶
- Return type:
Returns the file’s absolute path as a
QDir
object.See also
- PySide2.QtCore.QFileInfo.absoluteFilePath()¶
- Return type:
str
Returns an absolute path including the file name.
The absolute path name consists of the full path and the file name. On Unix this will always begin with the root, ‘/’, directory. On Windows this will always begin ‘D:/’ where D is a drive letter, except for network shares that are not mapped to a drive letter, in which case the path will begin ‘//sharename/’.
QFileInfo
will uppercase drive letters. Note thatQDir
does not do this. The code snippet below shows this.fi = QFileInfo("c:/temp/foo") => fi.absoluteFilePath() => "C:/temp/foo"
This function returns the same as
filePath()
, unlessisRelative()
is true. In contrast tocanonicalFilePath()
, symbolic links or redundant “.” or “..” elements are not necessarily removed.Warning
If
filePath()
is empty the behavior of this function is undefined.See also
- PySide2.QtCore.QFileInfo.absolutePath()¶
- Return type:
str
Returns a file’s path absolute path. This doesn’t include the file name.
On Unix the absolute path will always begin with the root, ‘/’, directory. On Windows this will always begin ‘D:/’ where D is a drive letter, except for network shares that are not mapped to a drive letter, in which case the path will begin ‘//sharename/’.
In contrast to
canonicalPath()
symbolic links or redundant “.” or “..” elements are not necessarily removed.Warning
If
filePath()
is empty the behavior of this function is undefined.
- PySide2.QtCore.QFileInfo.baseName()¶
- Return type:
str
Returns the base name of the file without the path.
The base name consists of all characters in the file up to (but not including) the first ‘.’ character.
Example:
fi = QFileInfo("/tmp/archive.tar.gz") base = fi.baseName() # base = "archive"
The base name of a file is computed equally on all platforms, independent of file naming conventions (e.g., “.bashrc” on Unix has an empty base name, and the suffix is “bashrc”).
- PySide2.QtCore.QFileInfo.birthTime()¶
- Return type:
Returns the date and time when the file was created / born.
If the file birth time is not available, this function returns an invalid
QDateTime
.If the file is a symlink, the time of the target file is returned (not the symlink).
See also
- PySide2.QtCore.QFileInfo.bundleName()¶
- Return type:
str
Returns the name of the bundle.
On macOS and iOS this returns the proper localized name for a bundle if the path
isBundle()
. On all other platforms an emptyQString
is returned.Example:
fi = QFileInfo("/Applications/Safari.app") bundle = fi.bundleName() # name = "Safari"
See also
- PySide2.QtCore.QFileInfo.caching()¶
- Return type:
bool
Returns
true
if caching is enabled; otherwise returnsfalse
.See also
- PySide2.QtCore.QFileInfo.canonicalFilePath()¶
- Return type:
str
Returns the canonical path including the file name, i.e. an absolute path without symbolic links or redundant “.” or “..” elements.
If the file does not exist, returns an empty string.
See also
- PySide2.QtCore.QFileInfo.canonicalPath()¶
- Return type:
str
Returns the file’s path canonical path (excluding the file name), i.e. an absolute path without symbolic links or redundant “.” or “..” elements.
If the file does not exist, returns an empty string.
See also
- PySide2.QtCore.QFileInfo.completeBaseName()¶
- Return type:
str
Returns the complete base name of the file without the path.
The complete base name consists of all characters in the file up to (but not including) the last ‘.’ character.
Example:
fi = QFileInfo("/tmp/archive.tar.gz") base = fi.completeBaseName() # base = "archive.tar"
See also
- PySide2.QtCore.QFileInfo.completeSuffix()¶
- Return type:
str
Returns the complete suffix (extension) of the file.
The complete suffix consists of all characters in the file after (but not including) the first ‘.’.
Example:
fi = QFileInfo("/tmp/archive.tar.gz") ext = fi.completeSuffix() # ext = "tar.gz"
See also
- PySide2.QtCore.QFileInfo.created()¶
- Return type:
Note
This function is deprecated.
Returns the date and time when the file was created, the time its metadata was last changed or the time of last modification, whichever one of the three is available (in that order).
This function is deprecated. Instead, use the
birthTime()
function to get the time the file was created,metadataChangeTime()
to get the time its metadata was last changed, orlastModified()
to get the time it was last modified.If the file is a symlink, the time of the target file is returned (not the symlink).
- PySide2.QtCore.QFileInfo.dir()¶
- Return type:
Returns the path of the object’s parent directory as a
QDir
object.Note
The
QDir
returned always corresponds to the object’s parent directory, even if theQFileInfo
represents a directory.For each of the following, returns the
QDir
"~/examples/191697"
.QFileInfo fileInfo1("~/examples/191697/."); QFileInfo fileInfo2("~/examples/191697/.."); QFileInfo fileInfo3("~/examples/191697/main.cpp");
For each of the following, returns the
QDir
"."
.QFileInfo fileInfo4("."); QFileInfo fileInfo5(".."); QFileInfo fileInfo6("main.cpp");
- PySide2.QtCore.QFileInfo.exists()¶
- Return type:
bool
Returns
true
if the file exists; otherwise returnsfalse
.Note
If the file is a symlink that points to a non-existing file, false is returned.
- static PySide2.QtCore.QFileInfo.exists(file)
- Parameters:
file – str
- Return type:
bool
Returns
true
if thefile
exists; otherwise returnsfalse
.Note
If
file
is a symlink that points to a non-existing file, false is returned.Note
Using this function is faster than using
QFileInfo(file).exists()
for file system access.
- PySide2.QtCore.QFileInfo.fileName()¶
- Return type:
str
Returns the name of the file, excluding the path.
Example:
fi = QFileInfo("/tmp/archive.tar.gz") name = fi.fileName() # name = "archive.tar.gz"
Note that, if this
QFileInfo
object is given a path ending in a slash, the name of the file is considered empty.See also
- PySide2.QtCore.QFileInfo.filePath()¶
- Return type:
str
Returns the file name, including the path (which may be absolute or relative).
- PySide2.QtCore.QFileInfo.group()¶
- Return type:
str
Returns the group of the file. On Windows, on systems where files do not have groups, or if an error occurs, an empty string is returned.
This function can be time consuming under Unix (in the order of milliseconds).
If the file is a symlink, this function returns the owning group of the target (not the symlink).
- PySide2.QtCore.QFileInfo.groupId()¶
- Return type:
uint
Returns the id of the group the file belongs to.
On Windows and on systems where files do not have groups this function always returns (uint) -2.
If the file is a symlink, this function returns the id of the group owning the target (not the symlink).
- PySide2.QtCore.QFileInfo.isAbsolute()¶
- Return type:
bool
Returns
true
if the file path is absolute, otherwise returnsfalse
(i.e. the path is relative).Note
Paths starting with a colon (: ) are always considered absolute, as they denote a
QResource
.See also
- PySide2.QtCore.QFileInfo.isBundle()¶
- Return type:
bool
Returns
true
if this object points to a bundle or to a symbolic link to a bundle on macOS and iOS; otherwise returnsfalse
.If the file is a symlink, this function returns true if the target is a bundle (not the symlink).
See also
- PySide2.QtCore.QFileInfo.isDir()¶
- Return type:
bool
Returns
true
if this object points to a directory or to a symbolic link to a directory; otherwise returnsfalse
.If the file is a symlink, this function returns true if the target is a directory (not the symlink).
See also
- PySide2.QtCore.QFileInfo.isExecutable()¶
- Return type:
bool
Returns
true
if the file is executable; otherwise returnsfalse
.If the file is a symlink, this function returns true if the target is executable (not the symlink).
See also
isReadable()
isWritable()
permission()
- PySide2.QtCore.QFileInfo.isFile()¶
- Return type:
bool
Returns
true
if this object points to a file or to a symbolic link to a file. Returnsfalse
if the object points to something which isn’t a file, such as a directory.If the file is a symlink, this function returns true if the target is a regular file (not the symlink).
See also
- PySide2.QtCore.QFileInfo.isHidden()¶
- Return type:
bool
Returns
true
if this is a `hidden’ file; otherwise returnsfalse
.Note
This function returns
true
for the special entries “.” and “..” on Unix, even thoughentryList
threats them as shown. And note that, since this function inspects the file name, on Unix it will inspect the name of the symlink, if this file is a symlink, not the target’s name.On Windows, this function returns
true
if the target file is hidden (not the symlink).
- PySide2.QtCore.QFileInfo.isJunction()¶
- Return type:
bool
Returns
true
if the object points to a junction; otherwise returnsfalse
.Junctions only exist on Windows’ NTFS file system, and are typically created by the
mklink
command. They can be thought of as symlinks for directories, and can only be created for absolute paths on the local volume.
- PySide2.QtCore.QFileInfo.isNativePath()¶
- Return type:
bool
Returns
true
if the file path can be used directly with native APIs. Returnsfalse
if the file is otherwise supported by a virtual file system inside Qt, such as the Qt Resource System .Note
Native paths may still require conversion of path separators and character encoding, depending on platform and input requirements of the native API.
- PySide2.QtCore.QFileInfo.isReadable()¶
- Return type:
bool
Returns
true
if the user can read the file; otherwise returnsfalse
.If the file is a symlink, this function returns true if the target is readable (not the symlink).
Note
If the
NTFS permissions
check has not been enabled, the result on Windows will merely reflect whether the file exists.See also
isWritable()
isExecutable()
permission()
- PySide2.QtCore.QFileInfo.isRelative()¶
- Return type:
bool
Returns
true
if the file path is relative, otherwise returnsfalse
(i.e. the path is absolute). (E.g. under Unix a path is absolute if it begins with a “/”).Note
Paths starting with a colon (: ) are always considered absolute, as they denote a
QResource
.See also
- PySide2.QtCore.QFileInfo.isRoot()¶
- Return type:
bool
Returns
true
if the object points to a directory or to a symbolic link to a directory, and that directory is the root directory; otherwise returnsfalse
.
- PySide2.QtCore.QFileInfo.isShortcut()¶
- Return type:
bool
Returns
true
if this object points to a shortcut; otherwise returnsfalse
.Shortcuts only exist on Windows and are typically
.lnk
files. For instance, true will be returned for shortcuts (*.lnk
files) on Windows, but false will be returned on Unix (including macOS and iOS).The shortcut (.lnk) files are treated as regular files. Opening those will open the
.lnk
file itself. In order to open the file a shortcut references to, it must usessymLinkTarget()
on a shortcut.Note
Even if a shortcut (broken shortcut) points to a non existing file, returns true.
See also
- PySide2.QtCore.QFileInfo.isSymLink()¶
- Return type:
bool
Returns
true
if this object points to a symbolic link, shortcut, or alias; otherwise returnsfalse
.Symbolic links exist on Unix (including macOS and iOS) and Windows and are typically created by the
ln -s
ormklink
commands, respectively. Opening a symbolic link effectively opens thelink's target
.In addition, true will be returned for shortcuts (
*.lnk
files) on Windows, and aliases on macOS. This behavior is deprecated and will likely change in a future version of Qt. Opening a shortcut or alias will open the.lnk
or alias file itself.Example:
info = QFileInfo(fileName) if info.isSymLink(): fileName = info.symLinkTarget()
Note
If the symlink points to a non existing file,
exists()
returns false.See also
- PySide2.QtCore.QFileInfo.isSymbolicLink()¶
- Return type:
bool
Returns
true
if this object points to a symbolic link; otherwise returnsfalse
.Symbolic links exist on Unix (including macOS and iOS) and Windows (NTFS-symlink) and are typically created by the
ln -s
ormklink
commands, respectively.Unix handles symlinks transparently. Opening a symbolic link effectively opens the
link's target
.In contrast to
isSymLink()
, false will be returned for shortcuts (*.lnk
files) on Windows and aliases on macOS. UseisShortcut()
on Windows instead.Note
If the symlink points to a non existing file,
exists()
returns false.See also
- PySide2.QtCore.QFileInfo.isWritable()¶
- Return type:
bool
Returns
true
if the user can write to the file; otherwise returnsfalse
.If the file is a symlink, this function returns true if the target is writeable (not the symlink).
Note
If the
NTFS permissions
check has not been enabled, the result on Windows will merely reflect whether the file is marked as Read Only.See also
isReadable()
isExecutable()
permission()
- PySide2.QtCore.QFileInfo.lastModified()¶
- Return type:
Returns the date and local time when the file was last modified.
If the file is a symlink, the time of the target file is returned (not the symlink).
See also
birthTime()
lastRead()
metadataChangeTime()
fileTime()
- PySide2.QtCore.QFileInfo.lastRead()¶
- Return type:
Returns the date and local time when the file was last read (accessed).
On platforms where this information is not available, returns the same as
lastModified()
.If the file is a symlink, the time of the target file is returned (not the symlink).
See also
birthTime()
lastModified()
metadataChangeTime()
fileTime()
- PySide2.QtCore.QFileInfo.makeAbsolute()¶
- Return type:
bool
Converts the file’s path to an absolute path if it is not already in that form. Returns
true
to indicate that the path was converted; otherwise returnsfalse
to indicate that the path was already absolute.See also
- PySide2.QtCore.QFileInfo.metadataChangeTime()¶
- Return type:
Returns the date and time when the file metadata was changed. A metadata change occurs when the file is created, but it also occurs whenever the user writes or sets inode information (for example, changing the file permissions).
If the file is a symlink, the time of the target file is returned (not the symlink).
See also
- PySide2.QtCore.QFileInfo.__ne__(fileinfo)¶
- Parameters:
fileinfo –
PySide2.QtCore.QFileInfo
- Return type:
bool
Returns
true
if thisQFileInfo
object refers to a different file than the one specified byfileinfo
; otherwise returnsfalse
.See also
operator==()
- PySide2.QtCore.QFileInfo.__eq__(fileinfo)¶
- Parameters:
fileinfo –
PySide2.QtCore.QFileInfo
- Return type:
bool
Returns
true
if thisQFileInfo
object refers to a file in the same location asfileinfo
; otherwise returnsfalse
.Note that the result of comparing two empty
QFileInfo
objects, containing no file references (file paths that do not exist or are empty), is undefined.Warning
This will not compare two different symbolic links pointing to the same file.
Warning
Long and short file names that refer to the same file on Windows are treated as if they referred to different files.
See also
operator!=()
- PySide2.QtCore.QFileInfo.owner()¶
- Return type:
str
Returns the owner of the file. On systems where files do not have owners, or if an error occurs, an empty string is returned.
This function can be time consuming under Unix (in the order of milliseconds). On Windows, it will return an empty string unless the
NTFS permissions
check has been enabled.If the file is a symlink, this function returns the owner of the target (not the symlink).
- PySide2.QtCore.QFileInfo.ownerId()¶
- Return type:
uint
Returns the id of the owner of the file.
On Windows and on systems where files do not have owners this function returns ((uint) -2).
If the file is a symlink, this function returns the id of the owner of the target (not the symlink).
- PySide2.QtCore.QFileInfo.path()¶
- Return type:
str
Returns the file’s path. This doesn’t include the file name.
Note that, if this
QFileInfo
object is given a path ending in a slash, the name of the file is considered empty and this function will return the entire path.
- PySide2.QtCore.QFileInfo.readLink()¶
- Return type:
str
Note
This function is deprecated.
Use
symLinkTarget()
instead.
- PySide2.QtCore.QFileInfo.refresh()¶
Refreshes the information about the file, i.e. reads in information from the file system the next time a cached property is fetched.
- PySide2.QtCore.QFileInfo.setCaching(on)¶
- Parameters:
on – bool
If
enable
is true, enables caching of file information. Ifenable
is false caching is disabled.When caching is enabled,
QFileInfo
reads the file information from the file system the first time it’s needed, but generally not later.Caching is enabled by default.
- PySide2.QtCore.QFileInfo.setFile(dir, file)¶
- Parameters:
dir –
PySide2.QtCore.QDir
file – str
This is an overloaded function.
Sets the file that the
QFileInfo
provides information about tofile
in directorydir
.If
file
includes a relative path, theQFileInfo
will also have a relative path.See also
- PySide2.QtCore.QFileInfo.setFile(file)
- Parameters:
file –
PySide2.QtCore.QFile
- PySide2.QtCore.QFileInfo.setFile(file)
- Parameters:
file – str
- PySide2.QtCore.QFileInfo.size()¶
- Return type:
int
Returns the file size in bytes. If the file does not exist or cannot be fetched, 0 is returned.
If the file is a symlink, the size of the target file is returned (not the symlink).
See also
- PySide2.QtCore.QFileInfo.suffix()¶
- Return type:
str
Returns the suffix (extension) of the file.
The suffix consists of all characters in the file after (but not including) the last ‘.’.
Example:
fi = QFileInfo("/tmp/archive.tar.gz") ext = fi.suffix(); # ext = "gz"
The suffix of a file is computed equally on all platforms, independent of file naming conventions (e.g., “.bashrc” on Unix has an empty base name, and the suffix is “bashrc”).
- PySide2.QtCore.QFileInfo.swap(other)¶
- Parameters:
other –
PySide2.QtCore.QFileInfo
Swaps this file info with
other
. This function is very fast and never fails.
- PySide2.QtCore.QFileInfo.symLinkTarget()¶
- Return type:
str
Returns the absolute path to the file or directory a symbolic link points to, or an empty string if the object isn’t a symbolic link.
This name may not represent an existing file; it is only a string.
exists()
returnstrue
if the symlink points to an existing file.See also
© 2022 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.