QDir

Synopsis

Functions

Static functions

Detailed Description

The PySide.QtCore.QDir class provides access to directory structures and their contents.

A PySide.QtCore.QDir is 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 PySide.QtCore.QDir can 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:/Documents and Settings")

On Windows, the second example above will be translated to C:\Documents and Settings when used to access files.

Examples of relative paths:

QDir("images/landscape.png")

You can use the PySide.QtCore.QDir.isRelative() or PySide.QtCore.QDir.isAbsolute() functions to check if a PySide.QtCore.QDir is using a relative or an absolute file path. Call PySide.QtCore.QDir.makeAbsolute() to convert a relative PySide.QtCore.QDir to an absolute one.

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 PySide.QtCore.QDir.count() . A string list of the names of all the entries in a directory can be obtained with PySide.QtCore.QDir.entryList() . If you need information about each entry, use PySide.QtCore.QDir.entryInfoList() to obtain a list of PySide.QtCore.QFileInfo objects.

Paths to files and directories within a directory can be constructed using PySide.QtCore.QDir.filePath() and PySide.QtCore.QDir.absoluteFilePath() . The PySide.QtCore.QDir.filePath() function returns a path to the specified file or directory relative to the path of the PySide.QtCore.QDir object; PySide.QtCore.QDir.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 PySide.QtCore.QDir.remove() function. Directories cannot be removed in the same way as files; use PySide.QtCore.QDir.rmdir() to remove them instead.

It is possible to reduce the number of entries returned by PySide.QtCore.QDir.entryList() and PySide.QtCore.QDir.entryInfoList() by applying filters to a PySide.QtCore.QDir object. 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 PySide.QtCore.QDir.setNameFilters() . Attribute filters consist of a bitwise OR combination of Filters, and these are specified when calling PySide.QtCore.QDir.setFilter() . The sort order is specified using PySide.QtCore.QDir.setSorting() with a bitwise OR combination of SortFlags .

You can test to see if a filename matches a filter using the PySide.QtCore.QDir.match() function.

Filter and sort order flags may also be specified when calling PySide.QtCore.QDir.entryList() and PySide.QtCore.QDir.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 PySide.QtCore.QDir objects. There are also corresponding functions for these that return strings:

PySide.QtCore.QDir PySide.QtCore.QString Return Value
PySide.QtCore.QDir.current() PySide.QtCore.QDir.currentPath() The application’s working directory
PySide.QtCore.QDir.home() PySide.QtCore.QDir.homePath() The user’s home directory
PySide.QtCore.QDir.root() PySide.QtCore.QDir.rootPath() The root directory
PySide.QtCore.QDir.temp() PySide.QtCore.QDir.tempPath() The system’s temporary directory

The PySide.QtCore.QDir.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 QCoreApplication.applicationDirPath() .

The PySide.QtCore.QDir.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 PySide.QtCore.QDir.canonicalPath() function.

Paths can also be simplified by using PySide.QtCore.QDir.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 PySide.QtCore.QDir.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():
    print "Cannot find the example directory"

(We could also use the static convenience function QFile.exists() .)

Traversing directories and reading a file:

dir = QDir.root()                 # "/"
if not dir.cd("tmp"):             # "/tmp"
    print "Cannot find the \"/tmp\" directory"
else:
    file = QFile(dir.filePath("ex1.txt"))   # "/tmp/ex1.txt"
    if !file.open(QIODevice.ReadWrite):
        print "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 PySide.QtCore import QDir, QCoreApplication
import sys

app = QCoreApplication(sys.argv)
directory = QDir()
directory.setFilter(QDir.Files | QDir.Hidden | QDir.NoSymLinks)
directory.setSorting(QDir.Size | QDir.Reversed)

for entry in directory.entryInfoList():
    print "%s %s" % (entry.size(), entry.fileName())

See also

PySide.QtCore.QFileInfo PySide.QtCore.QFile PySide.QtGui.QFileDialog QApplication.applicationDirPath() Find Files Example

class PySide.QtCore.QDir(arg__1)
class PySide.QtCore.QDir([path=""])
class PySide.QtCore.QDir(path, nameFilter[, sort=QDir.SortFlags(Name | IgnoreCase)[, filter=QDir.AllEntries]])
Parameters:
  • path – unicode
  • filterPySide.QtCore.QDir.Filters
  • nameFilter – unicode
  • arg__1PySide.QtCore.QDir
  • sortPySide.QtCore.QDir.SortFlags

Constructs a PySide.QtCore.QDir object that is a copy of the PySide.QtCore.QDir object for directory dir .

See also

PySide.QtCore.QDir.operator=()

Constructs a PySide.QtCore.QDir pointing to the given directory path . If path is empty the program’s working directory, (”.”), is used.

PySide.QtCore.QDir.Filter

This enum describes the filtering options available to PySide.QtCore.QDir ; e.g. for PySide.QtCore.QDir.entryList() and PySide.QtCore.QDir.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, .lnk files 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 PySide.QtCore.QDir will not filter out files based on their permissions, so PySide.QtCore.QDir.entryList() and PySide.QtCore.QDir.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 Files flags allows all files to be listed for which the application has read access, write access or both. If the Dirs and Drives flags 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 permissons for a directory, use the PySide.QtCore.QDir.entryInfoList() function to get the associated PySide.QtCore.QFileInfo objects and then use the QFileInfo::permissons() to obtain the permissions and ownership for each file.

PySide.QtCore.QDir.SortFlag

This enum describes the sort options available to PySide.QtCore.QDir , e.g. for PySide.QtCore.QDir.entryList() and PySide.QtCore.QDir.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.

PySide.QtCore.QDir.__reduce__()
Return type:PyObject
PySide.QtCore.QDir.absoluteFilePath(fileName)
Parameters:fileName – unicode
Return type:unicode

Returns the absolute path name of a file in the directory. Does not check if the file actually exists in the directory; but see PySide.QtCore.QDir.exists() . Redundant multiple separators or ”.” and ”..” directories in fileName are not removed (see PySide.QtCore.QDir.cleanPath() ).

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

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 PySide.QtCore.QDir.addResourceSearchPath(path)
Parameters:path – unicode

Use QDir.addSearchPath() with a prefix instead.

Adds path to the search paths searched in to find resources that are not specified with an absolute path. The default search path is to search only in the root (:/ ).

See also

The Qt Resource System

static PySide.QtCore.QDir.addSearchPath(prefix, path)
Parameters:
  • prefix – unicode
  • path – unicode

Adds path to the search path for prefix .

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

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 PySide.QtCore.QDir.absolutePath() returns. If the canonical path does not exist (normally due to dangling symbolic links) PySide.QtCore.QDir.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".
PySide.QtCore.QDir.cd(dirName)
Parameters:dirName – unicode
Return type:PySide.QtCore.bool

Changes the PySide.QtCore.QDir ‘s directory to dirName .

Returns true if the new directory exists and is readable; otherwise returns false. Note that the logical PySide.QtCore.QDir.cd() operation is not performed if the new directory does not exist.

Calling cd(”..”) is equivalent to calling PySide.QtCore.QDir.cdUp() .

PySide.QtCore.QDir.cdUp()
Return type:PySide.QtCore.bool

Changes directory by moving one directory up from the PySide.QtCore.QDir ‘s current directory.

Returns true if the new directory exists and is readable; otherwise returns false. Note that the logical PySide.QtCore.QDir.cdUp() operation is not performed if the new directory does not exist.

static PySide.QtCore.QDir.cleanPath(path)
Parameters:path – unicode
Return type:unicode

Removes all multiple directory separators “/” and resolves any ”.”s or ”..”s found in the path, path .

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

static PySide.QtCore.QDir.convertSeparators(pathName)
Parameters:pathName – unicode
Return type:unicode

Use QDir.toNativeSeparators() instead.

PySide.QtCore.QDir.count()
Return type:PySide.QtCore.uint

Returns the total number of directories and files in the directory.

Equivalent to PySide.QtCore.QDir.entryList() . PySide.QtCore.QDir.count() .

See also

PySide.QtCore.QDir.operator[]() PySide.QtCore.QDir.entryList()

static PySide.QtCore.QDir.current()
Return type:PySide.QtCore.QDir

Returns the application’s current directory.

The directory is constructed using the absolute path of the current directory, ensuring that its PySide.QtCore.QDir.path() will be the same as its PySide.QtCore.QDir.absolutePath() .

static PySide.QtCore.QDir.currentPath()
Return type:unicode

Returns the absolute path of the application’s current directory.

PySide.QtCore.QDir.dirName()
Return type:unicode

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 PySide.QtCore.QDir.exists() .

static PySide.QtCore.QDir.drives()
Return type:

Returns a list of the root directories on this system.

On Windows this returns a list of PySide.QtCore.QFileInfo objects containing “C:/”, “D:/”, etc. On other operating systems, it returns a list containing just one root directory (i.e. “/”).

PySide.QtCore.QDir.entryInfoList([filters=QDir.NoFilter[, sort=QDir.NoSort]])
Parameters:
  • filtersPySide.QtCore.QDir.Filters
  • sortPySide.QtCore.QDir.SortFlags
Return type:

PySide.QtCore.QDir.entryInfoList(nameFilters[, filters=QDir.NoFilter[, sort=QDir.NoSort]])
Parameters:
  • nameFilters – list of strings
  • filtersPySide.QtCore.QDir.Filters
  • sortPySide.QtCore.QDir.SortFlags
Return type:

PySide.QtCore.QDir.entryList(nameFilters[, filters=QDir.NoFilter[, sort=QDir.NoSort]])
Parameters:
  • nameFilters – list of strings
  • filtersPySide.QtCore.QDir.Filters
  • sortPySide.QtCore.QDir.SortFlags
Return type:

list of strings

PySide.QtCore.QDir.entryList([filters=QDir.NoFilter[, sort=QDir.NoSort]])
Parameters:
  • filtersPySide.QtCore.QDir.Filters
  • sortPySide.QtCore.QDir.SortFlags
Return type:

list of strings

PySide.QtCore.QDir.exists(name)
Parameters:name – unicode
Return type:PySide.QtCore.bool

Returns true if the file called name exists; otherwise returns false.

Unless name contains 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.

PySide.QtCore.QDir.exists()
Return type:PySide.QtCore.bool

This is an overloaded function.

Returns true if 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.

PySide.QtCore.QDir.filePath(fileName)
Parameters:fileName – unicode
Return type:unicode

Returns the path name of a file in the directory. Does not check if the file actually exists in the directory; but see PySide.QtCore.QDir.exists() . If the PySide.QtCore.QDir is relative the returned path name will also be relative. Redundant multiple separators or ”.” and ”..” directories in fileName are not removed (see PySide.QtCore.QDir.cleanPath() ).

PySide.QtCore.QDir.filter()
Return type:PySide.QtCore.QDir.Filters

Returns the value set by PySide.QtCore.QDir.setFilter()

static PySide.QtCore.QDir.fromNativeSeparators(pathName)
Parameters:pathName – unicode
Return type:unicode

Returns pathName using ‘/’ 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.

static PySide.QtCore.QDir.home()
Return type:PySide.QtCore.QDir

Returns the user’s home directory.

The directory is constructed using the absolute path of the home directory, ensuring that its PySide.QtCore.QDir.path() will be the same as its PySide.QtCore.QDir.absolutePath() .

See PySide.QtCore.QDir.homePath() for details.

static PySide.QtCore.QDir.homePath()
Return type:unicode

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:/Documents and Settings/Username

Use the PySide.QtCore.QDir.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:

Under non-Windows operating systems the HOME environment variable is used if it exists, otherwise the path returned by the PySide.QtCore.QDir.rootPath() .

On Symbian this typically returns “c:/data”, i.e. the same as native PathInfo::PhoneMemoryRootPath().

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

Returns true if the directory’s path is absolute; otherwise returns false. See PySide.QtCore.QDir.isAbsolutePath() .

static PySide.QtCore.QDir.isAbsolutePath(path)
Parameters:path – unicode
Return type:PySide.QtCore.bool

Returns true if path is absolute; returns false if it is relative.

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

Returns true if 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.

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

Returns true if the directory path is relative; otherwise returns false. (Under Unix a path is relative if it does not start with a “/”).

static PySide.QtCore.QDir.isRelativePath(path)
Parameters:path – unicode
Return type:PySide.QtCore.bool

Returns true if path is relative; returns false if it is absolute.

PySide.QtCore.QDir.isRoot()
Return type:PySide.QtCore.bool

Returns true if 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 PySide.QtCore.QDir.canonicalPath() , e.g.

dir = QDir("/tmp/root_link")
dir = dir.canonicalPath()
if dir.isRoot():
    print "It is a root link"
PySide.QtCore.QDir.makeAbsolute()
Return type:PySide.QtCore.bool

Converts the directory path to an absolute path. If it is already absolute nothing happens. Returns true if the conversion succeeded; otherwise returns false.

static PySide.QtCore.QDir.match(filters, fileName)
Parameters:
  • filters – list of strings
  • fileName – unicode
Return type:

PySide.QtCore.bool

This is an overloaded function.

Returns true if the fileName matches any of the wildcard (glob) patterns in the list of filters ; otherwise returns false. The matching is case insensitive.

static PySide.QtCore.QDir.match(filter, fileName)
Parameters:
  • filter – unicode
  • fileName – unicode
Return type:

PySide.QtCore.bool

Returns true if the fileName matches the wildcard (glob) pattern filter ; otherwise returns false. The filter may contain multiple patterns separated by spaces or semicolons. The matching is case insensitive.

PySide.QtCore.QDir.mkdir(dirName)
Parameters:dirName – unicode
Return type:PySide.QtCore.bool

Creates a sub-directory called dirName .

Returns true on success; otherwise returns false.

If the directory already exists when this function is called, it will return false.

PySide.QtCore.QDir.mkpath(dirPath)
Parameters:dirPath – unicode
Return type:PySide.QtCore.bool

Creates the directory path dirPath .

The function will create all parent directories necessary to create the directory.

Returns true if successful; otherwise returns false.

If the path already exists when this function is called, it will return true.

PySide.QtCore.QDir.nameFilters()
Return type:list of strings

Returns the string list set by PySide.QtCore.QDir.setNameFilters()

static PySide.QtCore.QDir.nameFiltersFromString(nameFilter)
Parameters:nameFilter – unicode
Return type:list of strings

Returns a list of name filters from the given nameFilter . (If there is more than one filter, each pair of filters is separated by a space or by a semicolon.)

PySide.QtCore.QDir.__ne__(dir)
Parameters:dirPySide.QtCore.QDir
Return type:PySide.QtCore.bool

Returns true if directory dir and this directory have 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:
    print "They differ"
PySide.QtCore.QDir.__eq__(dir)
Parameters:dirPySide.QtCore.QDir
Return type:PySide.QtCore.bool

Returns true if directory dir and this directory have 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:
    print "They're the same"
PySide.QtCore.QDir.operator[](arg__1)
Parameters:arg__1PySide.QtCore.int
Return type:unicode

Returns the file name at position pos in the list of file names. Equivalent to PySide.QtCore.QDir.entryList() .at(index). pos must be a valid index position in the list (i.e., 0 <= pos < PySide.QtCore.QDir.count() ).

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

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 PySide.QtCore.QDir.setPath() ).

PySide.QtCore.QDir.refresh()

Refreshes the directory information.

PySide.QtCore.QDir.relativeFilePath(fileName)
Parameters:fileName – unicode
Return type:unicode

Returns the path to fileName relative to the directory.

dir = QDir("/home/bob")

s = dir.relativeFilePath("images/file.jpg")         # s is "images/file.jpg"
s = dir.relativeFilePath("/home/mary/file.txt")     # s is "../mary/file.txt"
PySide.QtCore.QDir.remove(fileName)
Parameters:fileName – unicode
Return type:PySide.QtCore.bool

Removes the file, fileName .

Returns true if the file is removed successfully; otherwise returns false.

PySide.QtCore.QDir.rename(oldName, newName)
Parameters:
  • oldName – unicode
  • newName – unicode
Return type:

PySide.QtCore.bool

Renames a file or directory from oldName to newName , and returns true if successful; otherwise returns false.

On most file systems, PySide.QtCore.QDir.rename() fails only if oldName does not exist, if newName and oldName are not on the same partition or if a file with the new name already exists. However, there are also other reasons why PySide.QtCore.QDir.rename() can fail. For example, on at least one file system PySide.QtCore.QDir.rename() fails if newName points to an open file.

PySide.QtCore.QDir.rmdir(dirName)
Parameters:dirName – unicode
Return type:PySide.QtCore.bool

Removes the directory specified by dirName .

The directory must be empty for PySide.QtCore.QDir.rmdir() to succeed.

Returns true if successful; otherwise returns false.

PySide.QtCore.QDir.rmpath(dirPath)
Parameters:dirPath – unicode
Return type:PySide.QtCore.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 true if successful; otherwise returns false.

static PySide.QtCore.QDir.root()
Return type:PySide.QtCore.QDir

Returns the root directory.

The directory is constructed using the absolute path of the root directory, ensuring that its PySide.QtCore.QDir.path() will be the same as its PySide.QtCore.QDir.absolutePath() .

See PySide.QtCore.QDir.rootPath() for details.

static PySide.QtCore.QDir.rootPath()
Return type:unicode

Returns the absolute path of the root directory.

For Unix operating systems this returns “/”. For Windows and Symbian file systems this normally returns “c:/”. I.E. the root of the system drive.

static PySide.QtCore.QDir.searchPaths(prefix)
Parameters:prefix – unicode
Return type:list of strings

Returns the search paths for prefix .

static PySide.QtCore.QDir.separator()
Return type:PySide.QtCore.QChar

Returns the native directory separator: “/” under Unix (including Mac OS X) 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 PySide.QtCore.QDir.toNativeSeparators() .

static PySide.QtCore.QDir.setCurrent(path)
Parameters:path – unicode
Return type:PySide.QtCore.bool

Sets the application’s current working directory to path . Returns true if the directory was successfully changed; otherwise returns false.

PySide.QtCore.QDir.setFilter(filter)
Parameters:filterPySide.QtCore.QDir.Filters
PySide.QtCore.QDir.setNameFilters(nameFilters)
Parameters:nameFilters – list of strings

Sets the name filters used by PySide.QtCore.QDir.entryList() and PySide.QtCore.QDir.entryInfoList() to the list of filters specified by nameFilters .

Each name filter is a wildcard (globbing) filter that understands * and ? wildcards. (See QRegExp wildcard matching .)

For example, the following code sets three name filters on a PySide.QtCore.QDir to ensure that only files with extensions typically used for C++ source files are listed:

filters = ["*.cpp", "*.cxx", "*.cc"]
dir_.setNameFilters(filters)
PySide.QtCore.QDir.setPath(path)
Parameters:path – unicode

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 PySide.QtCore.QDir.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 PySide.QtCore.QDir.setSearchPaths(prefix, searchPaths)
Parameters:
  • prefix – unicode
  • searchPaths – list of strings

Sets or replaces Qt’s search paths for file names with the prefix prefix to 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”). prefix can 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", [QDir.homePath() + "/images"])
QDir.setSearchPaths("docs", [":/embeddedDocuments"])
...
pixmap = QPixmap("icons:undo.png")  # will look for undo.png in QDir::homePath() + "/images"
file = QFile("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 .

PySide.QtCore.QDir.setSorting(sort)
Parameters:sortPySide.QtCore.QDir.SortFlags
PySide.QtCore.QDir.sorting()
Return type:PySide.QtCore.QDir.SortFlags

Returns the value set by PySide.QtCore.QDir.setSorting()

See also

PySide.QtCore.QDir.setSorting() QDir.SortFlag

static PySide.QtCore.QDir.temp()
Return type:PySide.QtCore.QDir

Returns the system’s temporary directory.

The directory is constructed using the absolute path of the temporary directory, ensuring that its PySide.QtCore.QDir.path() will be the same as its PySide.QtCore.QDir.absolutePath() .

See PySide.QtCore.QDir.tempPath() for details.

static PySide.QtCore.QDir.tempPath()
Return type:unicode

Returns the absolute path of the system’s temporary directory.

On Unix/Linux systems this is the path in the TMPDIR environment variable or /tmp if TMPDIR is not defined. On Windows this is usually the path in the TEMP or TMP environment variable. Whether a directory separator is added to the end or not, depends on the operating system.

static PySide.QtCore.QDir.toNativeSeparators(pathName)
Parameters:pathName – unicode
Return type:unicode

Returns pathName with the ‘/’ separators converted to separators that are appropriate for the underlying operating system.

On Windows, toNativeSeparators(“c:/winnt/system32”) returns “c:winntsystem32”.

The returned string may be the same as the argument on some operating systems, for example on Unix.