博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
如何测试Python中是否存在文件或目录?
阅读量:2518 次
发布时间:2019-05-11

本文共 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。

Answered by Eric Z Ma.
埃里克·马(Eric Z Ma)回答。

翻译自:

转载地址:http://zqlwd.baihongyu.com/

你可能感兴趣的文章
牛客网暑期ACM多校训练营(第一场) - J Different Integers(线段数组or莫队)
查看>>
(转)AS3 面相对象 高级话题
查看>>
Missile
查看>>
关于kindedit和 Uedit后者兼容前者
查看>>
微软BI 之SSIS 系列 - 利用 SSIS 模板快速开发 SSIS Package
查看>>
eclipse中使用git上传到githup,报401 Authorization Required
查看>>
基于Golang打造一款开源的WAF网关
查看>>
POJ 2955 Brackets
查看>>
Python: execute an external program (zz)
查看>>
在T-SQL语句中访问远程数据库(openrowset/opendatasource/openquery)
查看>>
闭包2
查看>>
轮播图组件及vue-awesome-swiper的引入
查看>>
Java 笔记07
查看>>
POJ 3041 Asteroids (二分匹配)
查看>>
响应式布局
查看>>
缺陷跟踪系统Mantis之安装篇(转载)
查看>>
UI1_UITableViewHomeWork
查看>>
简单解析依赖注入(控制反转)在Spring中的应用
查看>>
NoSQL 简介及什么是AICD
查看>>
hibernate+mysql的连接池配置
查看>>