本文共 916 字,大约阅读时间需要 3 分钟。
In Bash, the [ -f ]
and [ -d ]
tests can test whether a file or a directory exist. What are the corresponding method for these tests?
在Bash中, [ -f ]
和[ -d ]
测试可以测试文件或目录是否存在。 这些测试对应的方法是什么?
For -f
:
对于-f
:
**os.path.isfile(path)**
Return True if path is an existing regular file. This follows symbolic links.
如果path是现有的常规文件,则返回True。 这遵循符号链接。
For -d
:
对于-d
:
**os.path.isdir(path)**
Return True if path is an existing directory. This follows symbolic links.
如果path是现有目录,则返回True。 这遵循符号链接。
As they follow symbolic links (in Bash, -d
and -f
do too), if you are interested symbolic links out, you can use
由于它们遵循符号链接(在Bash中, -d
和-f
也是如此),如果您有兴趣符号链接,则可以使用
**os.path.islink(path)**
Return True if path refers to a directory entry that is a symbolic link. Always False if symbolic links are not supported by the python runtime.
如果path引用的目录条目是符号链接,则返回True。 如果python运行时不支持符号链接,则始终为False。
翻译自:
转载地址:http://zqlwd.baihongyu.com/