Skip to content

PythonQt.Qt.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 (shared captured text)
absoluteFilePath function (self) -> str X.absoluteFilePath() -> str (shared captured text)
absolutePath function (self) -> str X.absolutePath() -> str (shared captured text)
baseName function (self) -> str X.baseName() -> str (shared captured text)
bundleName function (self) -> str X.bundleName() -> str (shared captured text)
caching function (self) -> bool X.caching() -> bool (shared captured text)
canonicalFilePath function (self) -> str X.canonicalFilePath() -> str (shared captured text)
canonicalPath function (self) -> str X.canonicalPath() -> str (shared captured text)
className function (self, *args: Any, **kwargs: Any) -> Any Return the classname of the object (shared captured text)
completeBaseName function (self) -> str X.completeBaseName() -> str (shared captured text)
completeSuffix function (self) -> str X.completeSuffix() -> str (shared captured text)
created function (self) -> Any X.created() -> PythonQt.QtCore.QDateTime (shared captured text)
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 (shared captured text)
exists function (self) -> bool X.exists() -> bool (shared captured text)
fileName function (self) -> str X.fileName() -> str (shared captured text)
filePath function (self) -> str X.filePath() -> str (shared captured text)
group function (self) -> str X.group() -> str (shared captured text)
groupId function (self) -> int X.groupId() -> int (shared captured text)
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 (shared captured text)
isBundle function (self) -> bool X.isBundle() -> bool (shared captured text)
isDir function (self) -> bool X.isDir() -> bool (shared captured text)
isExecutable function (self) -> bool X.isExecutable() -> bool (shared captured text)
isFile function (self) -> bool X.isFile() -> bool (shared captured text)
isHidden function (self) -> bool X.isHidden() -> bool (shared captured text)
isNativePath function (self) -> bool X.isNativePath() -> bool (shared captured text)
isReadable function (self) -> bool X.isReadable() -> bool (shared captured text)
isRelative function (self) -> bool X.isRelative() -> bool (shared captured text)
isRoot function (self) -> bool X.isRoot() -> bool (shared captured text)
isSymLink function (self) -> bool X.isSymLink() -> bool (shared captured text)
isWritable function (self) -> bool X.isWritable() -> bool (shared captured text)
lastModified function (self) -> Any X.lastModified() -> PythonQt.QtCore.QDateTime (shared captured text)
lastRead function (self) -> Any X.lastRead() -> PythonQt.QtCore.QDateTime (shared captured text)
makeAbsolute function (self) -> bool X.makeAbsolute() -> bool (shared captured text)
owner function (self) -> str X.owner() -> str (shared captured text)
ownerId function (self) -> int X.ownerId() -> int (shared captured text)
path function (self) -> str X.path() -> str (shared captured text)
permission function (self, permissions: Any) -> bool X.permission(permissions) -> bool (shared captured text)
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. (shared captured text)
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. (shared captured text)
setCaching function (self, on: bool) -> None If enable is true, enables caching of file information. (shared captured text)
setFile function (self, dir: Any, file: Any) -> None X.setFile(dir, file) (shared captured text)
size function (self) -> int X.size() -> int (shared captured text)
suffix function (self) -> str X.suffix() -> str (shared captured text)
swap function (self, other: Any) -> None Swaps this file info with other. (shared captured text)
symLinkTarget function (self) -> str X.symLinkTarget() -> str (shared captured text)