def
__eq__
(dir)
def
__ne__
(dir)
def
__reduce__
()
def
absoluteFilePath
(fileName)
def
absolutePath
()
def
canonicalPath
()
def
cd
(dirName)
def
cdUp
()
def
count
()
def
dirName
()
def
entryInfoList
([filters=QDir.NoFilter[, sort=QDir.NoSort]])
def
entryInfoList
(nameFilters[, filters=QDir.NoFilter[, sort=QDir.NoSort]])
def
entryList
([filters=QDir.NoFilter[, sort=QDir.NoSort]])
def
entryList
(nameFilters[, filters=QDir.NoFilter[, sort=QDir.NoSort]])
def
exists
()
def
exists
(name)
def
filePath
(fileName)
def
filter
()
def
isAbsolute
()
def
isEmpty
([filters=QDir.Filters(AllEntries | NoDotAndDotDot)])
def
isReadable
()
def
isRelative
()
def
isRoot
()
def
makeAbsolute
()
def
mkdir
(dirName)
def
mkpath
(dirPath)
def
nameFilters
()
def
operator=
(path)
def
operator[]
(arg__1)
def
path
()
def
refresh
()
def
relativeFilePath
(fileName)
def
remove
(fileName)
def
removeRecursively
()
def
rename
(oldName, newName)
def
rmdir
(dirName)
def
rmpath
(dirPath)
def
setFilter
(filter)
def
setNameFilters
(nameFilters)
def
setPath
(path)
def
setSorting
(sort)
def
sorting
()
def
swap
(other)
def
addResourceSearchPath
(path)
def
addSearchPath
(prefix, path)
def
cleanPath
(path)
def
current
()
def
currentPath
()
def
drives
()
def
fromNativeSeparators
(pathName)
def
home
()
def
homePath
()
def
isAbsolutePath
(path)
def
isRelativePath
(path)
def
listSeparator
()
def
match
(filter, fileName)
def
match
(filters, fileName)
def
nameFiltersFromString
(nameFilter)
def
root
()
def
rootPath
()
def
searchPaths
(prefix)
def
separator
()
def
setCurrent
(path)
def
setSearchPaths
(prefix, searchPaths)
def
temp
()
def
tempPath
()
def
toNativeSeparators
(pathName)
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 资源系统 .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:/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")可以使用
isRelative()orisAbsolute()functions to check if aQDiris using a relative or an absolute file path. CallmakeAbsolute()to convert a relativeQDirto an absolute one.
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 withentryList(). If you need information about each entry, useentryInfoList()to obtain a list ofQFileInfo对象。Paths to files and directories within a directory can be constructed using
filePath()andabsoluteFilePath()。filePath()function returns a path to the specified file or directory relative to the path of theQDirobject;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; usermdir()to remove them instead.It is possible to reduce the number of entries returned by
entryList()andentryInfoList()by applying filters to aQDirobject. 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 callingsetFilter(). The sort order is specified usingsetSorting()with a bitwise OR combination ofSortFlags.You can test to see if a filename matches a filter using the
match()函数。Filter and sort order flags may also be specified when calling
entryList()andentryInfoList()in order to override previously defined behavior.
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:
QStringReturn Value
The application’s working directory
The user’s home directory
The root directory
The system’s temporary directory
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().
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 containC:/, and possibly other drive letters such asD:/, depending on the configuration of the user’s system.
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()函数。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.
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
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 PySide2.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())
QDir
(
arg__1
)
¶
QDir([path=””])
QDir(path, nameFilter[, sort=QDir.SortFlags(Name | IgnoreCase)[, filter=QDir.AllEntries]])
- param filter
过滤器- param path
unicode
- param arg__1
- param nameFilter
unicode
- param sort
SortFlags
构造
QDir
with path
path
, that filters its entries by name using
nameFilter
and by attributes using
filters
. It also sorts the names using
sort
.
默认
nameFilter
is an empty string, which excludes nothing; the default
filters
is
AllEntries
, which also means exclude nothing. The default
sort
is
Name
|
IgnoreCase
, i.e. sort by name case-insensitively.
若
path
is an empty string,
QDir
uses “.” (the current directory). If
nameFilter
is an empty string,
QDir
uses the name filter “*” (all files).
注意:
path
need not exist.
PySide2.QtCore.QDir.
Filter
¶
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:
|
常量 |
描述 |
|---|---|
|
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,
|
|
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 value.
A default constructed
QDir
will 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
,
可写
,和
文件
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
entryInfoList()
function to get the associated
QFileInfo
objects and then use the QFileInfo::permissons() to obtain the permissions and ownership for each file.
PySide2.QtCore.QDir.
SortFlag
¶
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:
|
常量 |
描述 |
|---|---|
|
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 and Reversed, directories are still put first, but in reverse order; the files will be listed after the directories, again in reverse order.
PySide2.QtCore.QDir.
__reduce__
(
)
¶
PyObject
PySide2.QtCore.QDir.
absoluteFilePath
(
fileName
)
¶
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
exists()
. Redundant multiple separators or “.” and “..” directories in
fileName
are not removed (see
cleanPath()
).
PySide2.QtCore.QDir.
absolutePath
(
)
¶
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.
PySide2.QtCore.QDir.
addResourceSearchPath
(
path
)
¶
path – unicode
注意
此函数被弃用。
使用
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 (
:/
).
另请参阅
PySide2.QtCore.QDir.
addSearchPath
(
prefix
,
path
)
¶
prefix – unicode
path – unicode
添加
path
to the search path for
prefix
.
另请参阅
PySide2.QtCore.QDir.
canonicalPath
(
)
¶
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
absolutePath()
returns. If the canonical path does not exist (normally due to dangling symbolic links) 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".
PySide2.QtCore.QDir.
cd
(
dirName
)
¶
dirName – unicode
bool
改变
QDir
‘s directory to
dirName
.
返回
true
若新目录存在;否则返回
false
. Note that the logical operation is not performed if the new directory does not exist.
Calling cd(“..”) is equivalent to calling
cdUp()
.
另请参阅
PySide2.QtCore.QDir.
cdUp
(
)
¶
bool
通过上移一目录来改变目录从
QDir
‘s current directory.
返回
true
若新目录存在;否则返回
false
. Note that the logical operation is not performed if the new directory does not exist.
另请参阅
PySide2.QtCore.QDir.
cleanPath
(
path
)
¶
path – unicode
unicode
返回
path
with 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”.
PySide2.QtCore.QDir.
count
(
)
¶
uint
Returns the total number of directories and files in the directory.
相当于
entryList()
..
另请参阅
operator[]()
entryList()
PySide2.QtCore.QDir.
current
(
)
¶
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()
.
PySide2.QtCore.QDir.
currentPath
(
)
¶
unicode
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.
PySide2.QtCore.QDir.
dirName
(
)
¶
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
exists()
.
PySide2.QtCore.QDir.
drives
(
)
¶
Returns a list of the root directories on this system.
On Windows this returns a list of
QFileInfo
objects containing “C:/”, “D:/”, etc. On other operating systems, it returns a list containing just one root directory (i.e. “/”).
另请参阅
PySide2.QtCore.QDir.
entryInfoList
(
[
filters=QDir.NoFilter
[
,
sort=QDir.NoSort
]
]
)
¶
filters
–
过滤器
sort
–
SortFlags
这是重载函数。
Returns a list of
QFileInfo
objects 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
filters
and
sort
自变量。
Returns an empty list if the directory is unreadable, does not exist, or if nothing matches the specification.
PySide2.QtCore.QDir.
entryInfoList
(
nameFilters
[
,
filters=QDir.NoFilter
[
,
sort=QDir.NoSort
]
]
)
¶
nameFilters – 字符串列表
filters
–
过滤器
sort
–
SortFlags
Returns a list of
QFileInfo
objects 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
,和
sort
自变量。
Returns an empty list if the directory is unreadable, does not exist, or if nothing matches the specification.
PySide2.QtCore.QDir.
entryList
(
[
filters=QDir.NoFilter
[
,
sort=QDir.NoSort
]
]
)
¶
filters
–
过滤器
sort
–
SortFlags
字符串列表
这是重载函数。
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
filters
and
sort
自变量。
Returns an empty list if the directory is unreadable, does not exist, or if nothing matches the specification.
注意
To list symlinks that point to non existing files,
系统
must be passed to the filter.
PySide2.QtCore.QDir.
entryList
(
nameFilters
[
,
filters=QDir.NoFilter
[
,
sort=QDir.NoSort
]
]
)
¶
nameFilters – 字符串列表
filters
–
过滤器
sort
–
SortFlags
字符串列表
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
,和
sort
自变量。
Returns an empty list if the directory is unreadable, does not exist, or if nothing matches the specification.
PySide2.QtCore.QDir.
exists
(
)
¶
bool
这是重载函数。
返回
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.
PySide2.QtCore.QDir.
exists
(
name
)
¶
name – unicode
bool
返回
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.
PySide2.QtCore.QDir.
filePath
(
fileName
)
¶
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
exists()
。若
QDir
is relative the returned path name will also be relative. Redundant multiple separators or “.” and “..” directories in
fileName
are not removed (see
cleanPath()
).
PySide2.QtCore.QDir.
filter
(
)
¶
过滤器
Returns the value set by
setFilter()
另请参阅
PySide2.QtCore.QDir.
fromNativeSeparators
(
pathName
)
¶
pathName – unicode
unicode
返回
pathName
using ‘/’ as file separator. On Windows, for instance, (“
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.
PySide2.QtCore.QDir.
home
(
)
¶
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()
.
见
homePath()
了解细节。
PySide2.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
使用
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
USERPROFILE
环境变量。
The path formed by concatenating the
HOMEDRIVE
and
HOMEPATH
environment variables.
The path specified by the
HOME
环境变量。
The path returned by the
rootPath()
function (which uses the
SystemDrive
environment variable)
C:/
目录。
Under non-Windows operating systems the
HOME
environment variable is used if it exists, otherwise the path returned by the
rootPath()
.
PySide2.QtCore.QDir.
isAbsolute
(
)
¶
bool
返回
true
if the directory’s path is absolute; otherwise returns
false
。见
isAbsolutePath()
.
PySide2.QtCore.QDir.
isAbsolutePath
(
path
)
¶
path – unicode
bool
返回
true
if
path
is absolute; returns
false
if it is relative.
PySide2.QtCore.QDir.
isEmpty
(
[
filters=QDir.Filters(AllEntries | NoDotAndDotDot)
]
)
¶
filters
–
过滤器
bool
Returns whether the directory is empty.
相当于
count()
==
0
with filters
QDir::AllEntries
|
QDir::NoDotAndDotDot
, but faster as it just checks whether the directory contains at least one entry.
注意
Unless you set the
filters
flags to include
QDir::NoDotAndDotDot
(as the default value does), no directory is empty.
PySide2.QtCore.QDir.
isReadable
(
)
¶
bool
返回
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.
另请参阅
PySide2.QtCore.QDir.
isRelative
(
)
¶
bool
返回
true
if the directory path is relative; otherwise returns false. (Under Unix a path is relative if it does not start with a “/”).
PySide2.QtCore.QDir.
isRelativePath
(
path
)
¶
path – unicode
bool
返回
true
if
path
is relative; returns
false
if it is absolute.
PySide2.QtCore.QDir.
isRoot
(
)
¶
bool
返回
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
canonicalPath()
,如
dir = QDir("/tmp/root_link")
dir = dir.canonicalPath()
if dir.isRoot():
print "It is a root link"
另请参阅
PySide2.QtCore.QDir.
listSeparator
(
)
¶
QChar
Returns the native path list separator: ‘:’ under Unix and ‘;’ under Windows.
另请参阅
PySide2.QtCore.QDir.
makeAbsolute
(
)
¶
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
.
PySide2.QtCore.QDir.
match
(
filters
,
fileName
)
¶
filters – 字符串列表
fileName – unicode
bool
PySide2.QtCore.QDir.
match
(
filter
,
fileName
)
¶
filter – unicode
fileName – unicode
bool
PySide2.QtCore.QDir.
mkdir
(
dirName
)
¶
dirName – unicode
bool
Creates a sub-directory called
dirName
.
返回
true
当成功时;否则返回
false
.
If the directory already exists when this function is called, it will return false.
另请参阅
PySide2.QtCore.QDir.
mkpath
(
dirPath
)
¶
dirPath – unicode
bool
Creates the directory path
dirPath
.
The function will create all parent directories necessary to create the directory.
返回
true
若成功;否则返回
false
.
If the path already exists when this function is called, it will return true.
另请参阅
PySide2.QtCore.QDir.
nameFilters
(
)
¶
字符串列表
Returns the string list set by
setNameFilters()
另请参阅
PySide2.QtCore.QDir.
nameFiltersFromString
(
nameFilter
)
¶
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.)
PySide2.QtCore.QDir.
__ne__
(
dir
)
¶
dir
–
QDir
bool
返回
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"
PySide2.QtCore.QDir.operator=(path)
path – unicode
注意
此函数被弃用。
PySide2.QtCore.QDir.
__eq__
(
dir
)
¶
dir
–
QDir
bool
返回
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"
PySide2.QtCore.QDir.operator[](arg__1)
arg__1
–
int
unicode
Returns the file name at position
pos
in the list of file names. Equivalent to
entryList()
.at(index).
pos
must be a valid index position in the list (i.e., 0 <= pos <
count()
).
另请参阅
PySide2.QtCore.QDir.
path
(
)
¶
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
setPath()
).
PySide2.QtCore.QDir.
refresh
(
)
¶
Refreshes the directory information.
PySide2.QtCore.QDir.
relativeFilePath
(
fileName
)
¶
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"
PySide2.QtCore.QDir.
remove
(
fileName
)
¶
fileName – unicode
bool
Removes the file,
fileName
.
返回
true
if the file is removed successfully; otherwise returns
false
.
PySide2.QtCore.QDir.
removeRecursively
(
)
¶
bool
Removes the directory, including all its contents.
返回
true
if successful, otherwise false.
If a file or directory cannot be removed, 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.
PySide2.QtCore.QDir.
rename
(
oldName
,
newName
)
¶
oldName – unicode
newName – unicode
bool
Renames a file or directory from
oldName
to
newName
, and returns true if successful; otherwise returns
false
.
On most file systems, fails only if
oldName
does not exist, or if a file with the new name already exists. However, there are also other reasons why can fail. For example, on at least one file system fails if
newName
points to an open file.
若
oldName
is a file (not a directory) that can’t be renamed right away, Qt will try to copy
oldName
to
newName
and remove
oldName
.
另请参阅
PySide2.QtCore.QDir.
rmdir
(
dirName
)
¶
dirName – unicode
bool
Removes the directory specified by
dirName
.
The directory must be empty for to succeed.
返回
true
若成功;否则返回
false
.
另请参阅
PySide2.QtCore.QDir.
rmpath
(
dirPath
)
¶
dirPath – unicode
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).
返回
true
若成功;否则返回
false
.
另请参阅
PySide2.QtCore.QDir.
root
(
)
¶
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()
.
见
rootPath()
了解细节。
PySide2.QtCore.QDir.
rootPath
(
)
¶
unicode
Returns the absolute path of the root directory.
For Unix operating systems this returns “/”. For Windows file systems this normally returns “c:/”.
PySide2.QtCore.QDir.
searchPaths
(
prefix
)
¶
prefix – unicode
字符串列表
Returns the search paths for
prefix
.
PySide2.QtCore.QDir.
separator
(
)
¶
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()
.
另请参阅
PySide2.QtCore.QDir.
setCurrent
(
path
)
¶
path – unicode
bool
Sets the application’s current working directory to
path
。返回
true
if the directory was successfully changed; otherwise returns
false
.
PySide2.QtCore.QDir.
setFilter
(
filter
)
¶
filter
–
过滤器
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()
。见
Filter
.
另请参阅
PySide2.QtCore.QDir.
setNameFilters
(
nameFilters
)
¶
nameFilters – 字符串列表
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
QRegularExpression
Wildcard
Matching
.
For example, the following code sets three name filters on a
QDir
to ensure that only files with extensions typically used for C++ source files are listed:
filters = ["*.cpp", "*.cxx", "*.cc"]
dir_.setNameFilters(filters)
另请参阅
PySide2.QtCore.QDir.
setPath
(
path
)
¶
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
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”.
PySide2.QtCore.QDir.
setSearchPaths
(
prefix
,
searchPaths
)
¶
prefix – unicode
searchPaths – 字符串列表
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 资源系统 .
另请参阅
PySide2.QtCore.QDir.
setSorting
(
sort
)
¶
sort
–
SortFlags
Sets the sort order used by
entryList()
and
entryInfoList()
.
sort
is specified by OR-ing values from the enum
SortFlag
.
另请参阅
sorting()
SortFlag
PySide2.QtCore.QDir.
sorting
(
)
¶
SortFlags
Returns the value set by
setSorting()
另请参阅
setSorting()
SortFlag
PySide2.QtCore.QDir.
temp
(
)
¶
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()
.
见
tempPath()
了解细节。
PySide2.QtCore.QDir.
tempPath
(
)
¶
unicode
Returns the absolute canonical 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. The path returned by this method doesn’t end with a directory separator unless it is the root directory (of a drive).
PySide2.QtCore.QDir.
toNativeSeparators
(
pathName
)
¶
pathName – unicode
unicode
返回
pathName
with the ‘/’ separators converted to separators that are appropriate for the underlying operating system.
On Windows, (“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.