Python os.path.exists 和 os.path.isfile 的区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17752078/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 09:05:50  来源:igfitidea点击:

Difference between os.path.exists and os.path.isfile?

pythonfiledirectory

提问by Nate Rubin

I'm curious when these two functions will return different values. Also if you could clarify the difference between a path/directory/file that would be appreciated.

我很好奇这两个函数何时会返回不同的值。此外,如果您能澄清路径/目录/文件之间的区别,将不胜感激。

回答by scott_fakename

A directory tests true for exists, but false for isfile. There are a few different types of filesystem objects for which that is also true.

一个目录测试是否存在为真,但对于 isfile 为假。有几种不同类型的文件系统对象也是如此。

isfile means that it is a regular file. It is more specific than exists.

isfile 表示它是一个普通文件。它比现有的更具体。

回答by Dr. Jan-Philip Gehrcke

As you have already found out, the difference between existsand isfileis the fact that the former returns Truein case the given path is a directory ora file, while the latter only returns Trueif the path points to a file.

正如你已经发现,之间的差异exists,并isfile是一个事实,即前者返回True的情况下,给出的路径是一个目录文件,而后者只返回True,如果路径指向的文件。

Directoriesand filesare quite similar from the technical point of view. A file can contain any kind of data. A directory is just a special entry in the file system (at least on Unix operating systems it is just a special file) that represents the fact that it may contain files and other directories. It is a helpful means for building up a data structure. Using directories, you can organize your data in a hierarchical structure.

从技术角度来看,目录文件非常相似。文件可以包含任何类型的数据。目录只是文件系统中的一个特殊条目(至少在 Unix 操作系统上它只是一个特殊文件),代表它可能包含文件和其他目录的事实。它是构建数据结构的有用手段。使用目录,您可以按层次结构组织数据。

Especially in the Windows world, directories are often called "folders". I am sure that you yourself are using "folders" for organizing your files.

特别是在 Windows 世界中,目录通常被称为“文件夹”。我确信您自己正在使用“文件夹”来组织您的文件。

A pathis an unambiguous pointer to a resource in the file system. It can either point to a file or to a directory.

一个路径是一个明确的指针在文件系统中的资源。它可以指向一个文件或一个目录。