Table Of Contents

Previous topic

QDirIterator

Next topic

QTextDecoder

QFileInfo

Synopsis

Functions

Detailed Description

The PySide.QtCore.QFileInfo class provides system-independent file information.

PySide.QtCore.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. PySide.QtCore.QFileInfo can also be used to obtain information about a Qt resource .

A PySide.QtCore.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 function PySide.QtCore.QFileInfo.isRelative() to check whether a PySide.QtCore.QFileInfo is using a relative or an absolute file path. You can call the function PySide.QtCore.QFileInfo.makeAbsolute() to convert a relative PySide.QtCore.QFileInfo ‘s path to an absolute path.

The file that the PySide.QtCore.QFileInfo works on is set in the constructor or later with PySide.QtCore.QFileInfo.setFile() . Use PySide.QtCore.QFileInfo.exists() to see if the file exists and PySide.QtCore.QFileInfo.size() to get its size.

The file’s type is obtained with PySide.QtCore.QFileInfo.isFile() , PySide.QtCore.QFileInfo.isDir() and PySide.QtCore.QFileInfo.isSymLink() . The PySide.QtCore.QFileInfo.symLinkTarget() function provides the name of the file the symlink points to.

On Unix (including Mac OS X), the symlink has the same PySide.QtCore.QFileInfo.size() has the file it points to, because Unix handles symlinks transparently; similarly, opening a symlink using PySide.QtCore.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 56201

On Windows, symlinks (shortcuts) are .lnk files. The reported PySide.QtCore.QFileInfo.size() is that of the symlink (not the link’s target), and opening a symlink using PySide.QtCore.QFile opens the .lnk file. For example:

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 63942

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

The file’s dates are returned by PySide.QtCore.QFileInfo.created() , PySide.QtCore.QFileInfo.lastModified() and PySide.QtCore.QFileInfo.lastRead() . Information about the file’s access permissions is obtained with PySide.QtCore.QFileInfo.isReadable() , PySide.QtCore.QFileInfo.isWritable() and PySide.QtCore.QFileInfo.isExecutable() . The file’s ownership is available from PySide.QtCore.QFileInfo.owner() , PySide.QtCore.QFileInfo.ownerId() , PySide.QtCore.QFileInfo.group() and PySide.QtCore.QFileInfo.groupId() . You can examine a file’s permissions and ownership in a single statement using the PySide.QtCore.QFileInfo.permission() function.

Performance Issues

Some of PySide.QtCore.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, PySide.QtCore.QFileInfo.absolutePath() has to query the file system. The PySide.QtCore.QFileInfo.path() function, however, can work on the file name directly, and so it is faster.

Note

To speed up performance, PySide.QtCore.QFileInfo caches information about the file.

To speed up performance, PySide.QtCore.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: PySide.QtCore.QFileInfo.refresh() . If you want to switch off a PySide.QtCore.QFileInfo ‘s caching and force it to access the file system every time you request information from it call setCaching(false).

class PySide.QtCore.QFileInfo
class PySide.QtCore.QFileInfo(dir, file)
class PySide.QtCore.QFileInfo(file)
class PySide.QtCore.QFileInfo(fileinfo)
class PySide.QtCore.QFileInfo(file)
Parameters:

Constructs an empty PySide.QtCore.QFileInfo object.

Note that an empty PySide.QtCore.QFileInfo object contain no file reference.

Constructs a new PySide.QtCore.QFileInfo that gives information about the given file in the directory dir .

If dir has a relative path, the PySide.QtCore.QFileInfo will also have a relative path.

If file is an absolute path, then the directory specified by dir will be disregarded.

Constructs a new PySide.QtCore.QFileInfo that gives information about file file .

If the file has a relative path, the PySide.QtCore.QFileInfo will also have a relative path.

Constructs a new PySide.QtCore.QFileInfo that is a copy of the given fileinfo .

Constructs a new PySide.QtCore.QFileInfo that gives information about the given file. The file can also include an absolute or relative path.

PySide.QtCore.QFileInfo.__reduce__()
Return type:PyObject
PySide.QtCore.QFileInfo.absoluteDir()
Return type:PySide.QtCore.QDir

Returns the file’s absolute path as a PySide.QtCore.QDir object.

PySide.QtCore.QFileInfo.absoluteFilePath()
Return type:unicode

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/’. PySide.QtCore.QFileInfo will uppercase drive letters. Note that PySide.QtCore.QDir 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 PySide.QtCore.QFileInfo.filePath() , unless PySide.QtCore.QFileInfo.isRelative() is true. In contrast to PySide.QtCore.QFileInfo.canonicalFilePath() , symbolic links or redundant ”.” or ”..” elements are not necessarily removed.

If the PySide.QtCore.QFileInfo is empty it returns QDir.currentPath() .

PySide.QtCore.QFileInfo.absolutePath()
Return type:unicode

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 PySide.QtCore.QFileInfo.canonicalPath() symbolic links or redundant ”.” or ”..” elements are not necessarily removed.

Warning

If the PySide.QtCore.QFileInfo object was created with an empty PySide.QtCore.QString , the behavior of this function is undefined.

PySide.QtCore.QFileInfo.baseName()
Return type:unicode

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”).

PySide.QtCore.QFileInfo.bundleName()
Return type:unicode

Returns the name of the bundle.

On Mac OS X this returns the proper localized name for a bundle if the path PySide.QtCore.QFileInfo.isBundle() . On all other platforms an empty PySide.QtCore.QString is returned.

Example:

fi = QFileInfo("/Applications/Safari.app")
bundle = fi.bundleName()                # name = "Safari"
PySide.QtCore.QFileInfo.caching()
Return type:PySide.QtCore.bool

Returns true if caching is enabled; otherwise returns false.

PySide.QtCore.QFileInfo.canonicalFilePath()
Return type:unicode

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, PySide.QtCore.QFileInfo.canonicalFilePath() returns an empty string.

PySide.QtCore.QFileInfo.canonicalPath()
Return type:unicode

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, PySide.QtCore.QFileInfo.canonicalPath() returns an empty string.

PySide.QtCore.QFileInfo.completeBaseName()
Return type:unicode

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"
PySide.QtCore.QFileInfo.completeSuffix()
Return type:unicode

Returns the complete suffix 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"
PySide.QtCore.QFileInfo.created()
Return type:PySide.QtCore.QDateTime

Returns the date and time when the file was created.

On most Unix systems, this function returns the time of the last status change. A status 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 neither creation time nor “last status change” time are not available, returns the same as PySide.QtCore.QFileInfo.lastModified() .

PySide.QtCore.QFileInfo.dir()
Return type:PySide.QtCore.QDir

Returns the path of the object’s parent directory as a PySide.QtCore.QDir object.

Note

The PySide.QtCore.QDir returned always corresponds to the object’s parent directory, even if the PySide.QtCore.QFileInfo represents a directory.

For each of the following, PySide.QtCore.QFileInfo.dir() returns a PySide.QtCore.QDir for "~/examples/191697" .

fileInfo1 = QFileInfo("~/examples/191697/.")
fileInfo2 = QFileInfo("~/examples/191697/..")
fileInfo3 = QFileInfo("~/examples/191697/main.cpp")

For each of the following, PySide.QtCore.QFileInfo.dir() returns a PySide.QtCore.QDir for "." .

fileInfo4 = QFileInfo(".")
fileInfo5 = QFileInfo("..")
fileInfo6 = QFileInfo("main.cpp")
PySide.QtCore.QFileInfo.exists()
Return type:PySide.QtCore.bool

Returns true if the file exists; otherwise returns false.

Note

If the file is a symlink that points to a non existing file, false is returned.

PySide.QtCore.QFileInfo.fileName()
Return type:unicode

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 PySide.QtCore.QFileInfo object is given a path ending in a slash, the name of the file is considered empty.

PySide.QtCore.QFileInfo.filePath()
Return type:unicode

Returns the file name, including the path (which may be absolute or relative).

PySide.QtCore.QFileInfo.group()
Return type:unicode

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

PySide.QtCore.QFileInfo.groupId()
Return type:PySide.QtCore.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.

PySide.QtCore.QFileInfo.isAbsolute()
Return type:PySide.QtCore.bool

Returns true if the file path name is absolute, otherwise returns false if the path is relative.

PySide.QtCore.QFileInfo.isBundle()
Return type:PySide.QtCore.bool

Returns true if this object points to a bundle or to a symbolic link to a bundle on Mac OS X; otherwise returns false.

PySide.QtCore.QFileInfo.isDir()
Return type:PySide.QtCore.bool

Returns true if this object points to a directory or to a symbolic link to a directory; otherwise returns false.

PySide.QtCore.QFileInfo.isExecutable()
Return type:PySide.QtCore.bool

Returns true if the file is executable; otherwise returns false.

PySide.QtCore.QFileInfo.isFile()
Return type:PySide.QtCore.bool

Returns true if this object points to a file or to a symbolic link to a file. Returns false if the object points to something which isn’t a file, such as a directory.

PySide.QtCore.QFileInfo.isHidden()
Return type:PySide.QtCore.bool

Returns true if this is a `hidden’ file; otherwise returns false.

Note

This function returns true for the special entries ”.” and ”..” on Unix, even though QDir::entryList threats them as shown.

PySide.QtCore.QFileInfo.isReadable()
Return type:PySide.QtCore.bool

Returns true if the user can read the file; otherwise returns false.

PySide.QtCore.QFileInfo.isRelative()
Return type:PySide.QtCore.bool

Returns true if the file path name is relative, otherwise returns false if the path is absolute (e.g. under Unix a path is absolute if it begins with a “/”).

PySide.QtCore.QFileInfo.isRoot()
Return type:PySide.QtCore.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 returns false.

Return type:PySide.QtCore.bool

Returns true if this object points to a symbolic link (or to a shortcut on Windows); otherwise returns false.

On Unix (including Mac OS X), opening a symlink effectively opens the link's target . On Windows, it opens the .lnk file itself.

Example:

info = QFileInfo(fileName)
if info.isSymLink():
    fileName = info.symLinkTarget()

Note

If the symlink points to a non existing file, PySide.QtCore.QFileInfo.exists() returns false.

PySide.QtCore.QFileInfo.isWritable()
Return type:PySide.QtCore.bool

Returns true if the user can write to the file; otherwise returns false.

PySide.QtCore.QFileInfo.lastModified()
Return type:PySide.QtCore.QDateTime

Returns the date and time when the file was last modified.

PySide.QtCore.QFileInfo.lastRead()
Return type:PySide.QtCore.QDateTime

Returns the date and time when the file was last read (accessed).

On platforms where this information is not available, returns the same as PySide.QtCore.QFileInfo.lastModified() .

PySide.QtCore.QFileInfo.makeAbsolute()
Return type:PySide.QtCore.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 returns false to indicate that the path was already absolute.

PySide.QtCore.QFileInfo.__ne__(fileinfo)
Parameters:fileinfoPySide.QtCore.QFileInfo
Return type:PySide.QtCore.bool

This is an overloaded function.

PySide.QtCore.QFileInfo.__eq__(fileinfo)
Parameters:fileinfoPySide.QtCore.QFileInfo
Return type:PySide.QtCore.bool

This is an overloaded function.

PySide.QtCore.QFileInfo.owner()
Return type:unicode

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

PySide.QtCore.QFileInfo.ownerId()
Return type:PySide.QtCore.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).

PySide.QtCore.QFileInfo.path()
Return type:unicode

Returns the file’s path. This doesn’t include the file name.

Note that, if this PySide.QtCore.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.

PySide.QtCore.QFileInfo.permission(permissions)
Parameters:permissionsPySide.QtCore.QFile.Permissions
Return type:PySide.QtCore.bool
PySide.QtCore.QFileInfo.permissions()
Return type:PySide.QtCore.QFile.Permissions

Returns the complete OR-ed together combination of QFile.Permissions for the file.

Return type:unicode

Use PySide.QtCore.QFileInfo.symLinkTarget() instead.

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

Note

On Windows CE, there might be a delay for the file system driver to detect changes on the file.

PySide.QtCore.QFileInfo.setCaching(on)
Parameters:onPySide.QtCore.bool

If enable is true, enables caching of file information. If enable is false caching is disabled.

When caching is enabled, PySide.QtCore.QFileInfo reads the file information from the file system the first time it’s needed, but generally not later.

Caching is enabled by default.

PySide.QtCore.QFileInfo.setFile(dir, file)
Parameters:

This is an overloaded function.

Sets the file that the PySide.QtCore.QFileInfo provides information about to file in directory dir .

If file includes a relative path, the PySide.QtCore.QFileInfo will also have a relative path.

PySide.QtCore.QFileInfo.setFile(file)
Parameters:filePySide.QtCore.QFile

This is an overloaded function.

Sets the file that the PySide.QtCore.QFileInfo provides information about to file .

If file includes a relative path, the PySide.QtCore.QFileInfo will also have a relative path.

PySide.QtCore.QFileInfo.setFile(file)
Parameters:file – unicode

Sets the file that the PySide.QtCore.QFileInfo provides information about to file .

The file can also include an absolute or relative file path. Absolute paths begin with the directory separator (e.g. “/” under Unix) or a drive specification (under Windows). Relative file names begin with a directory name or a file name and specify a path relative to the current directory.

Example:

absolute = "/local/bin"
relative = "local/bin"
absFile = QFileInfo(absolute)
relFile = QFileInfo(relative)

QDir.setCurrent(QDir.rootPath())
# absFile and relFile now point to the same file

QDir.setCurrent("/tmp")
# absFile now points to "/local/bin",
# while relFile points to "/tmp/local/bin"
PySide.QtCore.QFileInfo.size()
Return type:PySide.QtCore.qint64

Returns the file size in bytes. If the file does not exist or cannot be fetched, 0 is returned.

PySide.QtCore.QFileInfo.suffix()
Return type:unicode

Returns the suffix 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”).

PySide.QtCore.QFileInfo.symLinkTarget()
Return type:unicode

Returns the absolute path to the file or directory a symlink (or shortcut on Windows) points to, or a an empty string if the object isn’t a symbolic link.

This name may not represent an existing file; it is only a string. QFileInfo.exists() returns true if the symlink points to an existing file.