【问题标题】:why os.path.isfile and os.path.islink return true for a soft link?为什么 os.path.isfile 和 os.path.islink 为软链接返回 true?
【发布时间】:2017-10-28 09:30:11
【问题描述】:

我正在尝试检查文件是软链接还是常规文件。但是当我使用 os.path.isfile 和 os.path.link 检查软链接时,这两个函数都返回 True。

In [34]: os.path.isfile('/bin/lessfile')
Out[34]: True

In [35]: os.path.islink('/bin/lessfile')
Out[35]: True

In [36]: ll /bin/lessfile
lrwxrwxrwx 1 root 8 Apr 29 15:22 /bin/lessfile -> lesspipe*

我的代码有什么问题吗?还是我遗漏了任何论点?

【问题讨论】:

  • 你想要 lstat。

标签: python python-3.x path operating-system


【解决方案1】:

islink 返回 True 因为 /bin/lessfile 是一个链接。 isfile 返回 True 因为链接指向文件。创建一个指向目录的链接并进行测试——isfile 将返回 False。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-29
    • 1970-01-01
    • 1970-01-01
    • 2021-11-10
    相关资源
    最近更新 更多