简体   繁体   English

除了文件和目录还有别的吗?

[英]Is there anything else besides files and directories?

I'm working on a project that iterates through directories and looks for files in windows.我正在开发一个遍历目录并在 windows 中查找文件的项目。

for element in os.listdir(path):
    new_path = os.path.join(path, element)
    if os.path.isdir(new_path):
         pass
    elif os.path.isfile(new_path):
         pass
    else:
         pass

Now what I'm curious about, is if windows or any other operating system has any other tree elements?现在我很好奇,如果 windows 或任何其他操作系统有任何其他树元素? If not is it still a good idea to check for that edge case?如果不是,检查边缘情况仍然是个好主意吗?

Checking path module documentation I see that there are multiple predicates:检查路径模块文档我看到有多个谓词:

  1. isfile()
  2. isdir()
  3. islink()

However when islink() is true , one of isdir() and isfile() is true too (since both of those follows symbolic links).但是,当islink()true时, isdir()isfile()之一也为true (因为它们都遵循符号链接)。

You have to be careful, that when there is no file or directory at path, both are false .您必须小心,当路径中没有文件或目录时,两者都是false You can verify that using exists() .您可以使用exists()验证这一点。 However in your example paths point to existing items, since you get them from listdir() .但是在您的示例路径中指向现有项目,因为您是从listdir()获取它们的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM