PySide6.QtCore.QDir¶
- class QDir¶
- The - QDirclass provides access to directory structures and their contents. More…- Synopsis¶- Methods¶- def - __init__()
- def - __reduce__()
- def - absolutePath()
- def - canonicalPath()
- def - cd()
- def - cdUp()
- def - count()
- def - dirName()
- def - entryInfoList()
- def - entryList()
- def - exists()
- def - filePath()
- def - filter()
- def - isAbsolute()
- def - isEmpty()
- def - isReadable()
- def - isRelative()
- def - isRoot()
- def - makeAbsolute()
- def - mkdir()
- def - mkpath()
- def - nameFilters()
- def - __ne__()
- def - __eq__()
- def - operator[]()
- def - path()
- def - refresh()
- def - remove()
- def - rename()
- def - rmdir()
- def - rmpath()
- def - setFilter()
- def - setNameFilters()
- def - setPath()
- def - setSorting()
- def - sorting()
- def - swap()
 - Static functions¶- def - addSearchPath()
- def - cleanPath()
- def - current()
- def - currentPath()
- def - drives()
- def - home()
- def - homePath()
- def - isAbsolutePath()
- def - isRelativePath()
- def - listSeparator()
- def - match()
- def - root()
- def - rootPath()
- def - searchPaths()
- def - separator()
- def - setCurrent()
- def - setSearchPaths()
- def - temp()
- def - tempPath()
 - Note - This documentation may contain snippets that were automatically translated from C++ to Python. We always welcome contributions to the snippet translation. If you see an issue with the translation, you can also let us know by creating a ticket on https:/bugreports.qt.io/projects/PYSIDE - Detailed Description¶- Warning - This section contains snippets that were automatically translated from C++ to Python and may contain errors. - A - QDiris used to manipulate path names, access information regarding paths and files, and manipulate the underlying file system. It can also be used to access Qt’s resource system .- Qt uses “/” as a universal directory separator in the same way that “/” is used as a path separator in URLs. If you always use “/” as a directory separator, Qt will translate your paths to conform to the underlying operating system. - A - QDircan point to a file using either a relative or an absolute path. Absolute paths begin with the directory separator (optionally preceded by 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.- Examples of absolute paths: - QDir("/home/user/Documents") QDir("C:/Users") - On Windows, the second example above will be translated to - C:\Userswhen used to access files.- Examples of relative paths: - QDir("images/landscape.png") - You can use the - isRelative()or- isAbsolute()functions to check if a- QDiris using a relative or an absolute file path. Call- makeAbsolute()to convert a relative- QDirto an absolute one.- Note - Paths starting with a colon (:) are always considered absolute, as they denote a - QResource.- Files and Directory Contents¶- Directories contain a number of entries, representing files, directories, and symbolic links. The number of entries in a directory is returned by - count(). A string list of the names of all the entries in a directory can be obtained with- entryList(). If you need information about each entry, use- entryInfoList()to obtain a list of- QFileInfoobjects.- Paths to files and directories within a directory can be constructed using - filePath()and- absoluteFilePath(). The- filePath()function returns a path to the specified file or directory relative to the path of the- QDirobject;- absoluteFilePath()returns an absolute path to the specified file or directory. Neither of these functions checks for the existence of files or directory; they only construct paths.- directory = QDir("Documents/Letters") path = directory.filePath("contents.txt") absolutePath = directory.absoluteFilePath("contents.txt") - Files can be removed by using the - remove()function. Directories cannot be removed in the same way as files; use- rmdir()to remove them instead.- It is possible to reduce the number of entries returned by - entryList()and- entryInfoList()by applying filters to a- QDirobject. You can apply a name filter to specify a pattern with wildcards that file names need to match, an attribute filter that selects properties of entries and can distinguish between files and directories, and a sort order.- Name filters are lists of strings that are passed to - setNameFilters(). Attribute filters consist of a bitwise OR combination of Filters, and these are specified when calling- setFilter(). The sort order is specified using- setSorting()with a bitwise OR combination of- SortFlags.- You can test to see if a filename matches a filter using the - match()function.- Filter and sort order flags may also be specified when calling - entryList()and- entryInfoList()in order to override previously defined behavior.- The Current Directory and Other Special Paths¶- Access to some common directories is provided with a number of static functions that return - QDirobjects. There are also corresponding functions for these that return strings:- QString- Return Value - The application’s working directory - The user’s home directory - The root directory - The system’s temporary directory - The - setCurrent()static function can also be used to set the application’s working directory.- If you want to find the directory containing the application’s executable, see - applicationDirPath().- The - drives()static function provides a list of root directories for each device that contains a filing system. On Unix systems this returns a list containing a single root directory “/”; on Windows the list will usually contain- C:/, and possibly other drive letters such as- D:/, depending on the configuration of the user’s system.- Path Manipulation and Strings¶- Paths containing “.” elements that reference the current directory at that point in the path, “..” elements that reference the parent directory, and symbolic links can be reduced to a canonical form using the - canonicalPath()function.- Paths can also be simplified by using - cleanPath()to remove redundant “/” and “..” elements.- It is sometimes necessary to be able to show a path in the native representation for the user’s platform. The static - toNativeSeparators()function returns a copy of the specified path in which each directory separator is replaced by the appropriate separator for the underlying operating system.- Examples¶- Check if a directory exists: - dir = QDir("example") if not dir.exists(): qWarning("Cannot find the example directory") - (We could also use one of the static convenience functions - exists()or- exists().)- Traversing directories and reading a file: - dir = QDir.root() # "/"() if not dir.cd("tmp"): # "/tmp" qWarning("Cannot find the \"/tmp\" directory") else: QFile file(dir.filePath("ex1.txt")) # "/tmp/ex1.txt" if not file.open(QIODevice.ReadWrite): qWarning("Cannot create the file %s", file.name()) - A program that lists all the files in the current directory (excluding symbolic links), sorted by size, smallest first: - from PySide6.QtCore import QDir if __name__ == "__main__": app = QCoreApplication(argc, argv) dir = QDir() dir.setFilter(QDir.Files | QDir.Hidden | QDir.NoSymLinks) dir.setSorting(QDir.Size | QDir.Reversed) list = dir.entryInfoList() print(" Bytes Filename") for i in range(0, list.size()): fileInfo = list.at(i) print(qPrintable(QString("%1 %2").arg(fileInfo.size(), 10)) .arg(fileInfo.fileName())) std::cout << std::endl return 0 - 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.
 - See also - QFileInfo- QFile- applicationDirPath()- Fetch More Example- class Filter¶
- (inherits - enum.Flag) This enum describes the filtering options available to- QDir; e.g. for- entryList()and- entryInfoList(). The filter value is specified by combining values from the following list using the bitwise OR operator:- Constant - Description - QDir.Dirs - List directories that match the filters. - QDir.AllDirs - List all directories; i.e. don’t apply the filters to directory names. - QDir.Files - List files. - QDir.Drives - List disk drives (ignored under Unix). - QDir.NoSymLinks - Do not list symbolic links (ignored by operating systems that don’t support symbolic links). - QDir.NoDotAndDotDot - Do not list the special entries “.” and “..”. - QDir.NoDot - Do not list the special entry “.”. - QDir.NoDotDot - Do not list the special entry “..”. - QDir.AllEntries - List directories, files, drives and symlinks (this does not list broken symlinks unless you specify System). - QDir.Readable - List files for which the application has read access. The Readable value needs to be combined with Dirs or Files. - QDir.Writable - List files for which the application has write access. The Writable value needs to be combined with Dirs or Files. - QDir.Executable - List files for which the application has execute access. The Executable value needs to be combined with Dirs or Files. - QDir.Modified - Only list files that have been modified (ignored on Unix). - QDir.Hidden - List hidden files (on Unix, files starting with a “.”). - QDir.System - List system files (on Unix, FIFOs, sockets and device files are included; on Windows, - .lnkfiles are included)- QDir.CaseSensitive - The filter should be case sensitive. - Functions that use Filter enum values to filter lists of files and directories will include symbolic links to files and directories unless you set the NoSymLinks value. - A default constructed - QDirwill not filter out files based on their permissions, so- entryList()and- entryInfoList()will return all files that are readable, writable, executable, or any combination of the three. This makes the default easy to write, and at the same time useful.- For example, setting the - Readable,- Writable, and- Filesflags allows all files to be listed for which the application has read access, write access or both. If the- Dirsand- Drivesflags are also included in this combination then all drives, directories, all files that the application can read, write, or execute, and symlinks to such files/directories can be listed.- To retrieve the permissions for a directory, use the - entryInfoList()function to get the associated- QFileInfoobjects and then use the- permissions()to obtain the permissions and ownership for each file.
 - class SortFlag¶
- (inherits - enum.Flag) This enum describes the sort options available to- QDir, e.g. for- entryList()and- entryInfoList(). The sort value is specified by OR-ing together values from the following list:- Constant - Description - QDir.Name - Sort by name. - QDir.Time - Sort by time (modification time). - QDir.Size - Sort by file size. - QDir.Type - Sort by file type (extension). - QDir.Unsorted - Do not sort. - QDir.NoSort - Not sorted by default. - QDir.DirsFirst - Put the directories first, then the files. - QDir.DirsLast - Put the files first, then the directories. - QDir.Reversed - Reverse the sort order. - QDir.IgnoreCase - Sort case-insensitively. - QDir.LocaleAware - Sort items appropriately using the current locale settings. - You can only specify one of the first four. - If you specify both DirsFirst and Reversed, directories are still put first, but in reverse order; the files will be listed after the directories, again in reverse order. 
 - Constructs a - QDirobject that is a copy of the- QDirobject for directory- dir.- See also - operator=()- __init__([path=""])
- Parameters:
- path – str 
 
 - Constructs a - QDirpointing to the given directory- path. If path is empty the program’s working directory, (“.”), is used.- See also - __init__(path, nameFilter[, sort=QDir.SortFlags(QDir.SortFlag.Name | QDir.SortFlag.IgnoreCase)[, filter=QDir.Filter.AllEntries]])
 - Constructs a - QDirwith path- path, that filters its entries by name using- nameFilterand by attributes using- filters. It also sorts the names using- sort.- The default - nameFilteris an empty string, which excludes nothing; the default- filtersis- AllEntries, which also excludes nothing. The default- sortis- Name|- IgnoreCase, i.e. sort by name case-insensitively.- If - pathis an empty string,- QDiruses “.” (the current directory). If- nameFilteris an empty string,- QDiruses the name filter “*” (all files).- __reduce__()¶
- Return type:
- str 
 
 - absoluteFilePath(fileName)¶
- Parameters:
- fileName – str 
- Return type:
- str 
 
 - Returns the absolute path name of a file in the directory. Does not check if the file actually exists in the directory; but see - exists(). Redundant multiple separators or “.” and “..” directories in- fileNameare not removed (see- cleanPath()).- See also - absolutePath()¶
- Return type:
- str 
 
 - Returns the absolute path (a path that starts with “/” or with a drive specification), which may contain symbolic links, but never contains redundant “.”, “..” or multiple separators. - static addSearchPath(prefix, path)¶
- Parameters:
- prefix – str 
- path – str 
 
 
 - Adds - pathto the search path for- prefix.- See also - canonicalPath()¶
- Return type:
- str 
 
 - Warning - This section contains snippets that were automatically translated from C++ to Python and may contain errors. - Returns the canonical path, i.e. a path without symbolic links or redundant “.” or “..” elements. - On systems that do not have symbolic links this function will always return the same string that - absolutePath()returns. If the canonical path does not exist (normally due to dangling symbolic links) canonicalPath() returns an empty string.- Example: - bin = "/local/bin" # where /local/bin is a symlink to /usr/bin binDir = QDir(bin) canonicalBin = binDir.canonicalPath() # canonicalBin now equals "/usr/bin" ls = "/local/bin/ls" # where ls is the executable "ls" lsDir = QDir(ls) canonicalLs = lsDir.canonicalPath() # canonicalLS now equals "/usr/bin/ls". - cd(dirName)¶
- Parameters:
- dirName – str 
- Return type:
- bool 
 
 - Changes the - QDir‘s directory to- dirName.- Returns - trueif the new directory exists; otherwise returns- false. Note that the logical cd() operation is not performed if the new directory does not exist.- Calling cd(“..”) is equivalent to calling - cdUp().- See also - cdUp()¶
- Return type:
- bool 
 
 - Changes directory by moving one directory up from the - QDir‘s current directory.- Returns - trueif the new directory exists; otherwise returns- false. Note that the logical cdUp() operation is not performed if the new directory does not exist.- Note - On Android, this is not supported for content URIs. For more information, see DocumentFile.getParentFile() . - See also - static cleanPath(path)¶
- Parameters:
- path – str 
- Return type:
- str 
 
 - Returns - pathwith directory separators normalized (that is, platform-native separators converted to “/”) and redundant ones removed, and “.”s and “..”s resolved (as far as possible).- Symbolic links are kept. This function does not return the canonical path, but rather the simplest version of the input. For example, “./local” becomes “local”, “local/../bin” becomes “bin” and “/local/usr/../bin” becomes “/local/bin”. - See also - count()¶
- Return type:
- int 
 
 - Returns the total number of directories and files in the directory. - Equivalent to - entryList().count().- Note - In Qt versions prior to 6.5, this function returned - uint, not- qsizetype.- See also - operator[]()- entryList()- Returns the application’s current directory. - The directory is constructed using the absolute path of the current directory, ensuring that its - path()will be the same as its- absolutePath().- See also - static currentPath()¶
- Return type:
- str 
 
 - Returns the absolute path of the application’s current directory. The current directory is the last directory set with - setCurrent()or, if that was never called, the directory at which this application was started at by the parent process.- dirName()¶
- Return type:
- str 
 
 - Returns the name of the directory; this is not the same as the path, e.g. a directory with the name “mail”, might have the path “/var/spool/mail”. If the directory has no name (e.g. it is the root directory) an empty string is returned. - No check is made to ensure that a directory with this name actually exists; but see - exists().- See also - Returns a list of the root directories on this system. - On Windows this returns a list of - QFileInfoobjects containing “C:/”, “D:/”, etc. This does not return drives with ejectable media that are empty. On other operating systems, it returns a list containing just one root directory (i.e. “/”).- See also - entryInfoList([filters=QDir.Filter.NoFilter[, sort=QDir.SortFlag.NoSort]])¶
 - This is an overloaded function. - Returns a list of - QFileInfoobjects for all the files and directories in the directory, ordered according to the name and attribute filters previously set with- setNameFilters()and- setFilter(), and sorted according to the flags set with- setSorting().- The attribute filter and sorting specifications can be overridden using the - filtersand- sortarguments.- Returns an empty list if the directory is unreadable, does not exist, or if nothing matches the specification. - entryInfoList(nameFilters[, filters=QDir.Filter.NoFilter[, sort=QDir.SortFlag.NoSort]])
 - Returns a list of - QFileInfoobjects for all the files and directories in the directory, ordered according to the name and attribute filters previously set with- setNameFilters()and- setFilter(), and sorted according to the flags set with- setSorting().- The name filter, file attribute filter, and sorting specification can be overridden using the - nameFilters,- filters, and- sortarguments.- Returns an empty list if the directory is unreadable, does not exist, or if nothing matches the specification. - entryList([filters=QDir.Filter.NoFilter[, sort=QDir.SortFlag.NoSort]])¶
 - This is an overloaded function. - Returns a list of the names of all the files and directories in the directory, ordered according to the name and attribute filters previously set with - setNameFilters()and- setFilter(), and sorted according to the flags set with- setSorting().- The attribute filter and sorting specifications can be overridden using the - filtersand- sortarguments.- Returns an empty list if the directory is unreadable, does not exist, or if nothing matches the specification. - Note - To list symlinks that point to non existing files, - Systemmust be passed to the filter.- entryList(nameFilters[, filters=QDir.Filter.NoFilter[, sort=QDir.SortFlag.NoSort]])
 - Returns a list of the names of all the files and directories in the directory, ordered according to the name and attribute filters previously set with - setNameFilters()and- setFilter(), and sorted according to the flags set with- setSorting().- The name filter, file attribute filter, and sorting specification can be overridden using the - nameFilters,- filters, and- sortarguments.- Returns an empty list if the directory is unreadable, does not exist, or if nothing matches the specification. - exists()¶
- Return type:
- bool 
 
 - This is an overloaded function. - Returns - trueif the directory exists; otherwise returns- false. (If a file with the same name is found this function will return false).- The overload of this function that accepts an argument is used to test for the presence of files and directories within a directory. - exists(name)
- Parameters:
- name – str 
- Return type:
- bool 
 
 - Returns - trueif the file called- nameexists; otherwise returns false.- Unless - namecontains an absolute file path, the file name is assumed to be relative to the directory itself, so this function is typically used to check for the presence of files within a directory.- filePath(fileName)¶
- Parameters:
- fileName – str 
- Return type:
- str 
 
 - Returns the path name of a file in the directory. Does not check if the file actually exists in the directory; but see - exists(). If the- QDiris relative the returned path name will also be relative. Redundant multiple separators or “.” and “..” directories in- fileNameare not removed (see- cleanPath()).- Returns the value set by - setFilter()- See also - static fromNativeSeparators(pathName)¶
- Parameters:
- pathName – str 
- Return type:
- str 
 
 - Returns - pathNameusing ‘/’ as file separator. On Windows, for instance, fromNativeSeparators(”- c:\\winnt\\system32") returns “c:/winnt/system32”.- The returned string may be the same as the argument on some operating systems, for example on Unix. - See also - Returns the user’s home directory. - The directory is constructed using the absolute path of the home directory, ensuring that its - path()will be the same as its- absolutePath().- See - homePath()for details.- static homePath()¶
- Return type:
- str 
 
 - Warning - This section contains snippets that were automatically translated from C++ to Python and may contain errors. - Returns the absolute path of the user’s home directory. - Under Windows this function will return the directory of the current user’s profile. Typically, this is: - C:/Users/Username - Use the - toNativeSeparators()function to convert the separators to the ones that are appropriate for the underlying operating system.- If the directory of the current user’s profile does not exist or cannot be retrieved, the following alternatives will be checked (in the given order) until an existing and available path is found: - The path specified by the - USERPROFILEenvironment variable.
- The path formed by concatenating the - HOMEDRIVEand- HOMEPATHenvironment variables.
- The path specified by the - HOMEenvironment variable.
- The path returned by the - rootPath()function (which uses the- SystemDriveenvironment variable)
- The - C:/directory.
 - Under non-Windows operating systems the - HOMEenvironment variable is used if it exists, otherwise the path returned by the- rootPath().- See also - isAbsolute()¶
- Return type:
- bool 
 
 - Returns - trueif the directory’s path is absolute; otherwise returns- false. See- isAbsolutePath().- Note - Paths starting with a colon (:) are always considered absolute, as they denote a - QResource.- See also - static isAbsolutePath(path)¶
- Parameters:
- path – str 
- Return type:
- bool 
 
 - Returns - trueif- pathis absolute; returns- falseif it is relative.- Note - Paths starting with a colon (:) are always considered absolute, as they denote a - QResource.- isEmpty([filters=QDir.Filters(QDir.Filter.AllEntries | QDir.Filter.NoDotAndDotDot)])¶
- Parameters:
- filters – Combination of - Filter
- Return type:
- bool 
 
 - Returns whether the directory is empty. - Equivalent to - count() == 0with filters- QDir::AllEntries | QDir::NoDotAndDotDot, but faster as it just checks whether the directory contains at least one entry.- Note - Unless you set the - filtersflags to include- QDir::NoDotAndDotDot(as the default value does), no directory is empty.- See also - isReadable()¶
- Return type:
- bool 
 
 - Returns - trueif the directory is readable and we can open files by name; otherwise returns- false.- Warning - A false value from this function is not a guarantee that files in the directory are not accessible. - See also - isRelative()¶
- Return type:
- bool 
 
 - Returns - trueif the directory path is relative; otherwise returns false. (Under Unix a path is relative if it does not start with a “/”).- Note - Paths starting with a colon (:) are always considered absolute, as they denote a - QResource.- static isRelativePath(path)¶
- Parameters:
- path – str 
- Return type:
- bool 
 
 - Returns - trueif- pathis relative; returns- falseif it is absolute.- Note - Paths starting with a colon (:) are always considered absolute, as they denote a - QResource.- See also - isRoot()¶
- Return type:
- bool 
 
 - Warning - This section contains snippets that were automatically translated from C++ to Python and may contain errors. - Returns - trueif the directory is the root directory; otherwise returns- false.- Note - If the directory is a symbolic link to the root directory this function returns - false. If you want to test for this use- canonicalPath(), e.g.- dir = QDir("/tmp/root_link") dir = dir.canonicalPath() if dir.isRoot(): qWarning("It is a root link") - See also - static listSeparator()¶
- Return type:
- QChar
 
 - Returns the native path list separator: ‘:’ under Unix and ‘;’ under Windows. - See also - makeAbsolute()¶
- Return type:
- bool 
 
 - Converts the directory path to an absolute path. If it is already absolute nothing happens. Returns - trueif the conversion succeeded; otherwise returns- false.- static match(filter, fileName)¶
- Parameters:
- filter – str 
- fileName – str 
 
- Return type:
- bool 
 
 - Returns - trueif the- fileNamematches the wildcard (glob) pattern- filter; otherwise returns- false. The- filtermay contain multiple patterns separated by spaces or semicolons. The matching is case insensitive.- See also - static match(filters, fileName)
- Parameters:
- filters – list of strings 
- fileName – str 
 
- Return type:
- bool 
 
 - This is an overloaded function. - Returns - trueif the- fileNamematches any of the wildcard (glob) patterns in the list of- filters; otherwise returns- false. The matching is case insensitive.- See also - mkdir(dirName)¶
- Parameters:
- dirName – str 
- Return type:
- bool 
 
 - This is an overloaded function. - Creates a sub-directory called - dirNamewith default permissions.- On POSIX systems the default is to grant all permissions allowed by - umask. On Windows, the new directory inherits its permissions from its parent directory.- mkdir(dirName, permissions)
- Parameters:
- dirName – str 
- permissions – Combination of - Permission
 
- Return type:
- bool 
 
 - Creates a sub-directory called - dirName.- Returns - trueon success; otherwise returns- false.- If the directory already exists when this function is called, it will return - false.- The permissions of the created directory are set to - permissions.- On POSIX systems the permissions are influenced by the value of - umask.- On Windows the permissions are emulated using ACLs. These ACLs may be in non-canonical order when the group is granted less permissions than others. Files and directories with such permissions will generate warnings when the Security tab of the Properties dialog is opened. Granting the group all permissions granted to others avoids such warnings. - See also - mkpath(dirPath)¶
- Parameters:
- dirPath – str 
- Return type:
- bool 
 
 - Creates the directory path - dirPath.- The function will create all parent directories necessary to create the directory. - Returns - trueif successful; otherwise returns- false.- If the path already exists when this function is called, it will return true. - See also - nameFilters()¶
- Return type:
- list of strings 
 
 - Returns the string list set by - setNameFilters()- See also - static nameFiltersFromString(nameFilter)¶
- Parameters:
- nameFilter – str 
- Return type:
- list of strings 
 
 - Warning - This section contains snippets that were automatically translated from C++ to Python and may contain errors. - Returns - trueif directory- lhsand directory- rhshave different paths or different sort or filter settings; otherwise returns- false.- Example: - # The current directory is "/usr/local" d1 = QDir("/usr/local/bin") d1.setFilter(QDir.Executable) d2 = QDir("bin") if d1 != d2: qDebug("They differ") - Warning - This section contains snippets that were automatically translated from C++ to Python and may contain errors. - Returns - trueif directory- lhsand directory- rhshave the same path and their sort and filter settings are the same; otherwise returns- false.- Example: - # The current directory is "/usr/local" d1 = QDir("/usr/local/bin") d2 = QDir("bin") if d1 == d2: qDebug("They're the same") - operator(pos)¶
- Parameters:
- pos – int 
- Return type:
- str 
 
 - Returns the file name at position - posin the list of file names. Equivalent to- entryList().at(index).- posmust be a valid index position in the list (i.e., 0 <= pos <- count()).- path()¶
- Return type:
- str 
 
 - Returns the path. This may contain symbolic links, but never contains redundant “.”, “..” or multiple separators. - The returned path can be either absolute or relative (see - setPath()).- refresh()¶
 - Refreshes the directory information. - relativeFilePath(fileName)¶
- Parameters:
- fileName – str 
- Return type:
- str 
 
 - Warning - This section contains snippets that were automatically translated from C++ to Python and may contain errors. - Returns the path to - fileNamerelative to the directory.- dir = QDir("/home/bob") s = QString() s = dir.relativeFilePath("images/file.jpg") # s is "images/file.jpg" s = dir.relativeFilePath("/home/mary/file.txt") # s is "../mary/file.txt" - See also - remove(fileName)¶
- Parameters:
- fileName – str 
- Return type:
- bool 
 
 - Removes the file, - fileName.- Returns - trueif the file is removed successfully; otherwise returns- false.- removeRecursively()¶
- Return type:
- bool 
 
 - Removes the directory, including all its contents. - Returns - trueif successful, otherwise false.- If a file or directory cannot be removed, removeRecursively() keeps going and attempts to delete as many files and sub-directories as possible, then returns - false.- If the directory was already removed, the method returns - true(expected result already reached).- Note - This function is meant for removing a small application-internal directory (such as a temporary directory), but not user-visible directories. For user-visible operations, it is rather recommended to report errors more precisely to the user, to offer solutions in case of errors, to show progress during the deletion since it could take several minutes, etc. - rename(oldName, newName)¶
- Parameters:
- oldName – str 
- newName – str 
 
- Return type:
- bool 
 
 - Renames a file or directory from - oldNameto- newName, and returns true if successful; otherwise returns- false.- On most file systems, rename() fails only if - oldNamedoes not exist, or if a file with the new name already exists. However, there are also other reasons why rename() can fail. For example, on at least one file system rename() fails if- newNamepoints to an open file.- If - oldNameis a file (not a directory) that can’t be renamed right away, Qt will try to copy- oldNameto- newNameand remove- oldName.- See also - rmdir(dirName)¶
- Parameters:
- dirName – str 
- Return type:
- bool 
 
 - Removes the directory specified by - dirName.- The directory must be empty for rmdir() to succeed. - Returns - trueif successful; otherwise returns- false.- See also - rmpath(dirPath)¶
- Parameters:
- dirPath – str 
- Return type:
- bool 
 
 - Removes the directory path - dirPath.- The function will remove all parent directories in - dirPath, provided that they are empty. This is the opposite of mkpath(dirPath).- Returns - trueif successful; otherwise returns- false.- See also - Returns the root directory. - The directory is constructed using the absolute path of the root directory, ensuring that its - path()will be the same as its- absolutePath().- See - rootPath()for details.- static rootPath()¶
- Return type:
- str 
 
 - Returns the absolute path of the root directory. - For Unix operating systems this returns “/”. For Windows file systems this normally returns “c:/”. - See also - static searchPaths(prefix)¶
- Parameters:
- prefix – str 
- Return type:
- list of strings 
 
 - Returns the search paths for - prefix.- See also - static separator()¶
- Return type:
- QChar
 
 - Returns the native directory separator: “/” under Unix and “\” under Windows. - You do not need to use this function to build file paths. If you always use “/”, Qt will translate your paths to conform to the underlying operating system. If you want to display paths to the user using their operating system’s separator use - toNativeSeparators().- See also - static setCurrent(path)¶
- Parameters:
- path – str 
- Return type:
- bool 
 
 - Warning - This section contains snippets that were automatically translated from C++ to Python and may contain errors. - Sets the application’s current working directory to - path. Returns- trueif the directory was successfully changed; otherwise returns- false.- 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" - See also - Sets the filter used by - entryList()and- entryInfoList()to- filters. The filter is used to specify the kind of files that should be returned by- entryList()and- entryInfoList(). See- Filter.- See also - setNameFilters(nameFilters)¶
- Parameters:
- nameFilters – list of strings 
 
 - Warning - This section contains snippets that were automatically translated from C++ to Python and may contain errors. - Sets the name filters used by - entryList()and- entryInfoList()to the list of filters specified by- nameFilters.- Each name filter is a wildcard (globbing) filter that understands - *and- ?wildcards. See- fromWildcard().- For example, the following code sets three name filters on a - QDirto ensure that only files with extensions typically used for C++ source files are listed:- filters = QStringList() filters << "*.cpp" << "*.cxx" << "*.cc" dir.setNameFilters(filters) - See also - setPath(path)¶
- Parameters:
- path – str 
 
 - Sets the path of the directory to - path. The path is cleaned of redundant “.”, “..” and of multiple separators. No check is made to see whether a directory with this path actually exists; but you can check for yourself using- exists().- The path can be either absolute or relative. Absolute paths begin with the directory separator “/” (optionally preceded by 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. An example of an absolute path is the string “/tmp/quartz”, a relative path might look like “src/fatlib”. - static setSearchPaths(prefix, searchPaths)¶
- Parameters:
- prefix – str 
- searchPaths – list of strings 
 
 
 - Warning - This section contains snippets that were automatically translated from C++ to Python and may contain errors. - Sets or replaces Qt’s search paths for file names with the prefix - prefixto- searchPaths.- To specify a prefix for a file name, prepend the prefix followed by a single colon (e.g., “images:undo.png”, “xmldocs:books.xml”). - prefixcan only contain letters or numbers (e.g., it cannot contain a colon, nor a slash).- Qt uses this search path to locate files with a known prefix. The search path entries are tested in order, starting with the first entry. - QDir.setSearchPaths("icons", QStringList(QDir.homePath() + "/images")) QDir.setSearchPaths("docs", QStringList(":/embeddedDocuments")) ... QPixmap pixmap("icons:undo.png") # will look for undo.png in QDir.homePath() + "/images" QFile file("docs:design.odf") # will look in the :/embeddedDocuments resource path - File name prefix must be at least 2 characters long to avoid conflicts with Windows drive letters. - Search paths may contain paths to The Qt Resource System . - See also - Sets the sort order used by - entryList()and- entryInfoList().- The - sortis specified by OR-ing values from the enum- SortFlag.- Returns the value set by - setSorting()- See also - Swaps this - QDirinstance with- other. This operation is very fast and never fails.- Returns the system’s temporary directory. - The directory is constructed using the absolute canonical path of the temporary directory, ensuring that its - path()will be the same as its- absolutePath().- See - tempPath()for details.- static tempPath()¶
- Return type:
- str 
 
 - Returns the absolute canonical path of the system’s temporary directory. - On Unix/Linux systems this is the path in the - TMPDIRenvironment variable or- /tmpif- TMPDIRis not defined. On Windows this is usually the path in the- TEMPor- TMPenvironment variable. The path returned by this method doesn’t end with a directory separator unless it is the root directory (of a drive).- See also - static toNativeSeparators(pathName)¶
- Parameters:
- pathName – str 
- Return type:
- str 
 
 - Returns - pathNamewith the ‘/’ separators converted to separators that are appropriate for the underlying operating system.- On Windows, toNativeSeparators(“c:/winnt/system32”) returns “c:\winnt\system32”. - The returned string may be the same as the argument on some operating systems, for example on Unix. - See also