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 资源系统 .
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 设置 when used to access files.
Examples of relative paths:
QDir("images/landscape.png")
可以使用 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.
A directory's path can be obtained with the PySide.QtCore.QDir.path() function, and a new path set with the PySide.QtCore.QDir.setPath() function. The absolute path to a directory is found by calling PySide.QtCore.QDir.absolutePath() .
The name of a directory is found using the PySide.QtCore.QDir.dirName() function. This typically returns the last element in the absolute path that specifies the location of the directory. However, it can also return ”.” if the PySide.QtCore.QDir represents the current directory.
QDir("Documents/Letters/Applications").dirName() # "Applications"
QDir().dirName() # "."
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:
| 常量 | 描述 |
|---|---|
| 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.
| 返回类型: | PyObject |
|---|
| 参数: | fileName – unicode |
|---|---|
| 返回类型: | 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() ).
| 返回类型: | 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.
| 参数: | path – unicode |
|---|
使用 QDir.addSearchPath() with a prefix instead.
添加 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 ( :/ ).
另请参阅
Qt 资源系统
| 参数: |
|
|---|
添加 path to the search path for prefix .
| 返回类型: | 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.
范例:
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".
| 参数: | dirName – unicode |
|---|---|
| 返回类型: | PySide.QtCore.bool |
改变 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.bool |
|---|
通过上移一目录来改变目录从 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.
| 参数: | path – unicode |
|---|---|
| 返回类型: | 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”.
| 参数: | pathName – unicode |
|---|---|
| 返回类型: | unicode |
使用 QDir.toNativeSeparators() 代替。
| 返回类型: | PySide.QtCore.uint |
|---|
Returns the total number of directories and files in the directory.
相当于 PySide.QtCore.QDir.entryList() . PySide.QtCore.QDir.count() .
另请参阅
PySide.QtCore.QDir.operator[]() PySide.QtCore.QDir.entryList()
| 返回类型: | 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() .
| 返回类型: | unicode |
|---|
Returns the absolute path of the application's current directory.
| 返回类型: | 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() .
| 返回类型: |
|---|
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. “/”).
| 参数: |
|
|---|---|
| 返回类型: |
| 参数: |
|
|---|---|
| 返回类型: |
| 参数: |
|
|---|---|
| 返回类型: |
字符串列表 |
| 参数: |
|
|---|---|
| 返回类型: |
字符串列表 |
| 参数: | name – unicode |
|---|---|
| 返回类型: | PySide.QtCore.bool |
Returns true if the file called name exists; otherwise returns false.
除非 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.bool |
|---|
这是重载函数。
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.
| 参数: | fileName – unicode |
|---|---|
| 返回类型: | 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() 。若 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.Filters |
|---|
Returns the value set by PySide.QtCore.QDir.setFilter()
| 参数: | pathName – unicode |
|---|---|
| 返回类型: | unicode |
返回 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.
| 返回类型: | 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() .
见 PySide.QtCore.QDir.homePath() 了解细节。
| 返回类型: | 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
使用 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.bool |
|---|
Returns true if the directory's path is absolute; otherwise returns false. See PySide.QtCore.QDir.isAbsolutePath() .
| 参数: | path – unicode |
|---|---|
| 返回类型: | PySide.QtCore.bool |
返回 true 若 path is absolute; returns false if it is relative.
| 返回类型: | PySide.QtCore.bool |
|---|
Returns true if the directory is readable and we can open files by name; otherwise returns false.
警告
A false value from this function is not a guarantee that files in the directory are not accessible.
| 返回类型: | 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 “/”).
| 参数: | path – unicode |
|---|---|
| 返回类型: | PySide.QtCore.bool |
返回 true 若 path is relative; returns false if it is absolute.
| 返回类型: | 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() ,如
dir = QDir("/tmp/root_link")
dir = dir.canonicalPath()
if dir.isRoot():
print "It is a root link"
| 返回类型: | 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.
| 参数: |
|
|---|---|
| 返回类型: |
PySide.QtCore.bool |
这是重载函数。
返回 true 若 fileName matches any of the wildcard (glob) patterns in the list of filters ; otherwise returns false. The matching is case insensitive.
另请参阅
QRegExp wildcard matching QRegExp.exactMatch() PySide.QtCore.QDir.entryList() PySide.QtCore.QDir.entryInfoList()
| 参数: |
|
|---|---|
| 返回类型: |
PySide.QtCore.bool |
返回 true 若 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.
另请参阅
QRegExp wildcard matching QRegExp.exactMatch() PySide.QtCore.QDir.entryList() PySide.QtCore.QDir.entryInfoList()
| 参数: | dirName – unicode |
|---|---|
| 返回类型: | PySide.QtCore.bool |
Creates a sub-directory called dirName .
返回 true 当成功时;否则返回 false。
If the directory already exists when this function is called, it will return false.
| 参数: | dirPath – unicode |
|---|---|
| 返回类型: | 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.
| 返回类型: | 字符串列表 |
|---|
Returns the string list set by PySide.QtCore.QDir.setNameFilters()
| 参数: | nameFilter – unicode |
|---|---|
| 返回类型: | 字符串列表 |
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.)
| 参数: | dir – PySide.QtCore.QDir |
|---|---|
| 返回类型: | PySide.QtCore.bool |
Returns true if directory dir and this directory have different paths or different sort or filter settings; otherwise returns false.
范例:
// The current directory is "/usr/local"
d1 = QDir("/usr/local/bin")
d1.setFilter(QDir.Executable)
d2 = QDir("bin")
if d1 != d2:
print "They differ"
| 参数: | dir – PySide.QtCore.QDir |
|---|---|
| 返回类型: | 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.
范例:
# The current directory is "/usr/local"
d1 = QDir("/usr/local/bin")
d2 = QDir("bin")
if d1 == d2:
print "They're the same"
| 参数: | arg__1 – PySide.QtCore.int |
|---|---|
| 返回类型: | 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() ).
| 返回类型: | 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() ).
Refreshes the directory information.
| 参数: | fileName – unicode |
|---|---|
| 返回类型: | 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"
| 参数: | fileName – unicode |
|---|---|
| 返回类型: | PySide.QtCore.bool |
Removes the file, fileName .
Returns true if the file is removed successfully; otherwise returns false.
| 参数: |
|
|---|---|
| 返回类型: |
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.
| 参数: | dirName – unicode |
|---|---|
| 返回类型: | 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.
| 参数: | dirPath – unicode |
|---|---|
| 返回类型: | 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.
| 返回类型: | 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() .
见 PySide.QtCore.QDir.rootPath() 了解细节。
| 返回类型: | 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.
| 参数: | prefix – unicode |
|---|---|
| 返回类型: | 字符串列表 |
Returns the search paths for prefix .
| 返回类型: | 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() .
| 参数: | path – unicode |
|---|---|
| 返回类型: | PySide.QtCore.bool |
Sets the application's current working directory to path . Returns true if the directory was successfully changed; otherwise returns false.
| 参数: | filter – PySide.QtCore.QDir.Filters |
|---|
| 参数: | 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)
| 参数: | 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”.
| 参数: |
|
|---|
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 Qt 资源系统 .
| 参数: | sort – PySide.QtCore.QDir.SortFlags |
|---|
| 返回类型: | PySide.QtCore.QDir.SortFlags |
|---|
Returns the value set by PySide.QtCore.QDir.setSorting()
另请参阅
PySide.QtCore.QDir.setSorting() QDir.SortFlag
| 返回类型: | 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() .
见 PySide.QtCore.QDir.tempPath() 了解细节。
| 返回类型: | 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.
| 参数: | pathName – unicode |
|---|---|
| 返回类型: | unicode |
返回 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.