Skip to content

PythonQt.QtCore.QFileInfo

QFileInfo

PythonQtInstanceWrapper object

Native bases: PythonQtInstanceWrapper

Supplemental documentation: Qt 5 reference:

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 "/temporary/quartz". A relative path might look like "src/fatlib". You can use the function isRelative() to check whether a QFileInfo is using a relative or an absolute file path. You can call the function makeAbsolute() to convert a relative QFileInfo'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 with setFile(). Use exists() to see if the file exists and size() to get its size.

The file's type is obtained with isFile(), isDir() and isSymLink(). The symLinkTarget() 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:

#ifdef Q_OS_UNIX

QFileInfo info1("/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"

QFileInfo info2(info1.symLinkTarget());
info2.isSymLink();          // returns false
info2.absoluteFilePath();   // returns "/opt/pretty++/bin/untabify"
info2.size();               // returns 56201

#endif

On 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.

#ifdef Q_OS_WIN

QFileInfo info1("C:\\Users\\Bob\\untabify.lnk");
info1.isSymLink();          // returns true
info1.absoluteFilePath();   // returns "C:/example-users/Bob/untabify.lnk"
info1.size();               // returns 63942
info1.symLinkTarget();      // returns "C:/Pretty++/untabify"

QFileInfo info2(info1.symLinkTarget());
info2.isSymLink();          // returns false
info2.absoluteFilePath();   // returns "C:/Pretty++/untabify"
info2.size();               // returns 63942

#endif

Elements of the file's name can be extracted with path() and fileName(). The fileName()'s parts can be extracted with baseName(), suffix() or completeSuffix(). 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 or setFile().

The file's dates are returned by birthTime(), lastModified(), lastRead() and fileTime(). Information about the file's access permissions is obtained with isReadable(), isWritable() and isExecutable(). The file's ownership is available from owner(), ownerId(), group() and groupId(). You can examine a file's permissions and ownership in a single statement using the permission() 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++; // turn checking on
qt_ntfs_permission_lookup--; // turn it off again

Note: Since this is a non-atomic global variable, it is only safe to increment or decrement qt_ntfs_permission_lookup before any threads other than the main thread have started or after every thread other than the main thread has ended.

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. The path() 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 a QFileInfo's caching and force it to access the file system every time you request information from it call setCaching(false).

Platform Specific Issues

On Android, some limitations apply when dealing with content URIs:

  • Access permissions might be needed by prompting the user through the QFileDialog which implements Android's native file picker.
  • Aim to follow the Scoped storage guidelines, such as using app specific directories instead of other public external directories. For more information, also see storage best practices.
  • Due to the design of Qt APIs (e.g. QFile), it's not possible to fully integrate the latter APIs with Android's MediaStore APIs.

Source: https://doc.qt.io/archives/qt-5.15/qfileinfo.html

Complete member index

Every captured member retains its own identity. Identical captured declaration text is linked once; this does not establish native inheritance or interchangeable objects.

Member Kind Signature Summary
__init__ function (self, *args: Any, **kwargs: Any) -> None Native constructor; consult class documentation for supported overloads. (shared captured text)
absoluteDir function (self) -> Any X.absoluteDir() -> PythonQt.QtCore.QDir
absoluteFilePath function (self) -> str X.absoluteFilePath() -> str
absolutePath function (self) -> str X.absolutePath() -> str
baseName function (self) -> str X.baseName() -> str
bundleName function (self) -> str X.bundleName() -> str
caching function (self) -> bool X.caching() -> bool
canonicalFilePath function (self) -> str X.canonicalFilePath() -> str
canonicalPath function (self) -> str X.canonicalPath() -> str
className function (self, *args: Any, **kwargs: Any) -> Any Return the classname of the object (shared captured text)
completeBaseName function (self) -> str X.completeBaseName() -> str
completeSuffix function (self) -> str X.completeSuffix() -> str
created function (self) -> Any X.created() -> PythonQt.QtCore.QDateTime
delete function (self, *args: Any, **kwargs: Any) -> Any Deletes the C++ object (at your own risk, my friend!) (shared captured text)
dir function (self) -> Any X.dir() -> PythonQt.QtCore.QDir
exists function (self) -> bool X.exists() -> bool
fileName function (self) -> str X.fileName() -> str
filePath function (self) -> str X.filePath() -> str
group function (self) -> str X.group() -> str
groupId function (self) -> int X.groupId() -> int
help function (self, *args: Any, **kwargs: Any) -> Any Shows the help of available methods for this class (shared captured text)
inherits function (self, *args: Any, **kwargs: Any) -> Any Returns if the class inherits or is of given type name (shared captured text)
isAbsolute function (self) -> bool X.isAbsolute() -> bool
isBundle function (self) -> bool X.isBundle() -> bool
isDir function (self) -> bool X.isDir() -> bool
isExecutable function (self) -> bool X.isExecutable() -> bool
isFile function (self) -> bool X.isFile() -> bool
isHidden function (self) -> bool X.isHidden() -> bool
isNativePath function (self) -> bool X.isNativePath() -> bool
isReadable function (self) -> bool X.isReadable() -> bool
isRelative function (self) -> bool X.isRelative() -> bool
isRoot function (self) -> bool X.isRoot() -> bool
isSymLink function (self) -> bool X.isSymLink() -> bool
isWritable function (self) -> bool X.isWritable() -> bool
lastModified function (self) -> Any X.lastModified() -> PythonQt.QtCore.QDateTime
lastRead function (self) -> Any X.lastRead() -> PythonQt.QtCore.QDateTime
makeAbsolute function (self) -> bool X.makeAbsolute() -> bool
owner function (self) -> str X.owner() -> str
ownerId function (self) -> int X.ownerId() -> int
path function (self) -> str X.path() -> str
permission function (self, permissions: Any) -> bool X.permission(permissions) -> bool
permissions function (self) -> Any Returns the complete OR-ed together combination of [QFile::Permissions](https://doc.qt.io/archives/qt-5.15/qfiledevice.html#Permission-enum) for the file.
refresh function (self) -> None Refreshes the information about the file, i.e. reads in information from the file system the next time a cached property is fetched.
setCaching function (self, on: bool) -> None If enable is true, enables caching of file information.
setFile function (self, dir: Any, file: Any) -> None X.setFile(dir, file)
size function (self) -> int X.size() -> int
suffix function (self) -> str X.suffix() -> str
swap function (self, other: Any) -> None Swaps this file info with other.
symLinkTarget function (self) -> str X.symLinkTarget() -> str