路径组件的命名标准是什么?
当我操作路径和文件名时,我总是陷入困境,因为我不遵循路径组件的命名标准。
考虑以下玩具问题(Windows 示例,但希望答案应该与平台无关)。您已获得一个文件夹的路径:
C:\Users\OddThinking\Documents\My Source\
您想要遍历下面的文件夹并将所有 .src 文件编译为 .obj 文件。
在某些时候,您正在查看以下路径:
C:\Users\OddThinking\Documents\My Source\Widget\foo.src
您将如何命名以下路径组件?
A. foo
B. foo.src
C. src
D. .src
E. C:\Users\OddThinking\Documents\My Source\ (absolute path of the root)
F. Widget\foo.src (relative path of the file to absolute path of the root)
G. Widget\
H. C:\Users\OddThinking\Documents\My Source\Widget\
I. C:\Users\OddThinking\Documents\My Source\Widget\foo.src
这是我的尝试:
A. 基本名称?基名?
B、文件名?文件名?
在选择标识符名称时,差异很重要,但我在这里从来没有保持一致。
C. 延期?
D. 延期?等等,这就是我所说的 C。
我是否应该避免存储点,而在需要时才将其放入?
如果特定文件上没有点怎么办?
E.?
F。?
G、文件夹?但这不是 Windows 特定的术语吗?
H. 路径名称?路径名?小路?
一、文件名?等等,这就是我所说的B。
路径名称?等等,这就是我所说的H。
I keep getting myself in knots when I am manipulating paths and file names because I don’t follow a naming standard for path components.
Consider the following toy problem (Windows example, but hopefully the answer should be platform independent). You have been given the path of a folder:
C:\Users\OddThinking\Documents\My Source\
You want to walk the folders underneath and compile all the .src files to .obj files.
At some point you are looking at the following path:
C:\Users\OddThinking\Documents\My Source\Widget\foo.src
How would you name the following path components?
A. foo
B. foo.src
C. src
D. .src
E. C:\Users\OddThinking\Documents\My Source\ (absolute path of the root)
F. Widget\foo.src (relative path of the file to absolute path of the root)
G. Widget\
H. C:\Users\OddThinking\Documents\My Source\Widget\
I. C:\Users\OddThinking\Documents\My Source\Widget\foo.src
Here is my attempt:
A. Base name? Basename?
B. File name? Filename?
The difference is important when choosing identifier names, and I am never consistent here.
C. Extension?
D. Extension? Wait, that is what I called C.
Should I avoid storing the dot, and just put it in when required?
What if there is no dot on a particular file?
E. ?
F. ?
G. Folder? But isn’t this a Windows-specific term?
H. Path name? Pathname? Path?
I. File name? Wait, that is what I called B.
Path name? Wait, that is what I called H.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
我认为您对“标准”命名约定的搜索将是徒劳的。以下是我根据现有的知名项目提出的建议。
I think your search for a "standard" naming convention will be in vain. Below are my proposals, based on existing, well-known programs.
首先,好问题,我的+1。当我不得不在 Utility 类中创建大量函数时,这件事让我很烦恼。获取文件名?或获取全名? GetApplicationPath 是指完整路径还是目录名?等等。我来自 .NET 背景,所以我想我可以为 @blinry 的优秀答案添加更多内容。
摘要:(斜体是我作为程序员不会使用的内容)
路径:路径指定文件系统中的唯一位置(除非它的相对路径)。路径名不太常用,但我会坚持使用路径 - 它几乎解释了它是什么。路径可以指向文件或文件夹,甚至不指向任何内容(C:\)。路径可以是:
My Source\Widget\
是相对路径,Widget\foo.src
也是相对路径。不言自明。C:\users\OddThinking\Documents\My Source\Widget\foo.src
因此是完整路径。请参阅最后我所说的指向文件并以目录结尾的完整路径。wiki 页面和路径的 .NET 命名是一致的.
根路径或根目录:前者是 . NET 约定,而后者在 UNIX 圈中更为常见。虽然我两者都喜欢,但我更倾向于使用前者。与 UNIX 不同,Windows 中有许多不同的根路径,每个分区都有一个根路径。 Unix 系统有一个根目录,其中保存其他目录和文件的信息。例如。
C:\
是根路径。文件夹或文件夹名称:
Widget
、OddThinking
等(根据您的情况)。这可能是 Windows 独有的约定(事实上这是我自己奇怪的想法:)),尽管如此,我强烈反对 blinry 的答案“目录”。虽然对于普通用户目录意味着与文件夹相同(如子文件夹、子目录),但我相信技术角度的“目录”听起来应该像是目标的合格地址,而不是目标本身。更多内容见下文。用户
而言,OddThinking
和Documents
是子文件夹。用户
、OddThinking\
、OddThinking\Documents\
和OddThinking \Documents\My Source\Widget\
是子目录。但我们并不经常需要为此操心,不是吗?用户
OddThinking
是子文件夹(以及子文件夹)OddThinking
,users
是其父文件夹(只是提到不同的术语,没什么大不了的)。目录或目录名称:前者在现实生活中普遍使用,后者在代码中使用。这是指直到目标的父文件夹为止的完全限定路径(或简称为完整路径)。在您的情况下,
C:\users\OddThinking\Documents\My Source\Widget
(是的,目录永远不会指向文件)。我在代码中使用目录名称,因为目录是 .NET 中的一个类,目录名称是库本身的名称。它与 UNIX 系统中使用的 dirname 非常一致。文件名或基本名称:文件的名称随着延伸。在您的情况下:
foo.src
。我想说,对于非技术用途,我更喜欢文件名(这对最终用户来说意味着什么),但出于技术目的,我会严格坚持基本名称。 MS 经常使用文件名,但令我惊讶的是它们不仅在文档中不一致,甚至在库中也不一致。文件名可以表示文件的基本名称或完整路径。所以我喜欢基本名称,这就是我在代码中对它们的称呼。 wiki 上的此页面也表示文件名可以表示完整路径或基本名称。令人惊讶的是,即使在 .NET 中,我也可以找到使用 basename 来表示文件的根名称。扩展名或文件扩展名或文件扩展名:我喜欢最后一个。所有都指的是同一件事,但它是什么又是一个争论的问题! Wiki 说它是
src
而当时我记得读过许多语言将其解释为.src
。注意点。因此,我的看法是,对于临时用途,它是什么并不重要,但作为一名程序员,我总是将扩展名视为.src
。好吧,我可能尝试获取一些标准用法,但这是我遵循的两个约定。它与完整路径有关。
我通常将指向文件的完整路径称为文件路径。对我来说,文件路径很明确,它告诉我它是什么。虽然对于文件名,我发现它是文件的名称,但在我的代码中,我将其称为文件名。它也与“目录名称”一致。从技术角度来看,名称指的是完全限定名称!令人沮丧的是 .NET 使用术语文件名(所以我在这里有我的情况),有时也使用文件路径。
我将以目录结尾的完整路径称为目录。事实上,我们可以将任何不指向文件的地址称为目录。所以
C:\users\OddThinking\Documents\My Source\
是一个目录,C:\users\OddThinking\
是一个目录,甚至OddThinking\Documents \My Source\
(最好将其称为子目录,甚至更好的相对路径 - 所有这一切都取决于您正在处理它的上下文)。上面我提到了一些关于目录的不同之处,即目录名称。这是我的看法:我将找到一条新的路径来避免混乱。D:\Fruit\Apple\Pip\
是什么?一个目录。但如果问题是D:\Fruit\Apple\Pip\
的目录或者更好的目录名称是什么,答案是D:\Fruit\Apple\
。希望它清楚。我想说最好不要担心最后两个术语,因为这会造成最大的混乱(对我个人而言)。只需使用术语完整路径!
回答你:
关于你给出的路径
A) 不知道。无论如何,我从来不需要单独得到那个。
B) 基本名称
C)我暂时将其称为文件扩展名,我最不担心,因为我从来不需要在代码中单独命名它。
D) 文件扩展名肯定是。
E) 我认为这不是通用目的要求。不知道。在.NET 中,基本目录与目录名称相同。
F) 相对路径
G) 文件夹(基本名称
foo.src
的父文件夹)H) 目录名称
I)完整路径(甚至文件名)
一般情况下(抱歉,我是有点冗长,只是为了让大家明白这一点)但假设 foo.src 确实是一个文件
A) 不适用
B) 基本名称
C) 不适用
D) 扩展
E) 目录或简单路径
F) 相对路径
G) 不适用
H) 目录或简单路径
I) 完整路径(甚至文件名)
用我这边的一个例子进一步说明:
考虑路径
C:\Documents and Settings\All Users\Application Data\s.sql
。C:\Documents and Settings\All Users\Application Data\s.sql
是完整路径(即文件名)C:\Documents and Settings\All Users\Application Data\
是目录名称。现在考虑路径
C:\Documents and Settings\All Users\Application Data
C:\Documents and Settings\All Users\Application Data
是完整路径(恰好是一个目录)C:\Documents and Settings\All Users
是目录名称。我的两个提示:
我遵循这条经验法则,即当涉及到寻址完整地址时,无论其类型如何,我几乎总是将其称为“完整路径” ”。这不仅消除了文件路径和文件夹路径这两个术语的使用,而且还避免了如果您要将文件名称命名为文件名(对于大多数用户而言,立即转换为基本名称)可能会产生混淆。但是,是的,如果您必须具体说明路径类型,最好命名文件名或目录,而不是更通用的“路径”。
无论是什么,你都会有自己的想法,并始终保持一致。团队成员之间达成共识,这意味着这个而不是那个。
现在,刚刚从圈子里出来的我已经有了一些练习。 OS X 和 Android 机器上使用的术语将是一个新品牌。所有这些都只是文件系统中的物理路径。就网址而言,将会出现一套全新的术语。我希望有人能填补同一条线索中的空白:)我很高兴听到您所进行的约定..
Good question first of all, my +1. This thing bugged me when I had to create a slew of functions in Utility class once. GetFileName? or GetFullName? GetApplicationPath means full path or the directory name? and so on. I come from .NET background, so I think I can add little more to otherwise excellent answer by @blinry.
Summary: (In italics is what I would not use as a programmer)
Path: Path specifies a unique location in the file system (unless its relative path). Path name is less often used, but I would stick with path - it pretty much explains what it is. Path can point to a file or a folder or even nothing (C:\). Path can be:
My Source\Widget\
is relative path as well asWidget\foo.src
. Self explanatory.C:\users\OddThinking\Documents\My Source\Widget\foo.src
is hence full path. See at the end what I call full path that points to a file and that ends as a directory.The wiki page and .NET naming for path is consistent.
Root Path or Root Directory: Former is .NET convention while latter is more heard in UNIX circles. Though I like both I tend to use the former more. In windows, unlike UNIX, has many different root paths, one for each partition. Unix systems have one root directory which holds information on other directories and files. Eg.
C:\
is root path.Folder or Folder Name:
Widget
,OddThinking
etc in your case. This might be a Windows only convention (in fact its my own odd thinking :)), nevertheless I strongly object to blinry`s answer "Directory". Though for a normal user directory means the same as a folder (like subfolders, subdirectories), I believe from a technical angle "directory" should sound like a qualified address to the target and not the target itself. More below.users
OddThinking
andDocuments
are sub folders.users
OddThinking\
,OddThinking\Documents\
andOddThinking\Documents\My Source\Widget\
are sub directories. But we do not often need to bother about it, do we?users
OddThinking
is a child folder (as well as sub folder)OddThinking
users
is its parent folder (Just mentioning different terminologies, no big deal).Directory or Directory Name: The former to use generally in real life, the latter to be in code. This refers to the fully qualified path (or simply full path) till the target's parent folder. In your case,
C:\users\OddThinking\Documents\My Source\Widget
(Yes a directory is never meant to point to a file). I use directory name in my code since directory is a class in .NET and Directory Name is what the library itself calls it. Its quite consistent with dirname used in UNIX systems.File Name or Basename: Name of the file along with extension. In your case:
foo.src
. I would say that for a non technical use I prefer file name (it is what it means for an end user) but for technical purposes I would strictly stick with basename. File Name is often used by MS, but I am surprised how they are not consistent not just in documentation but even in library. There filename could mean either basename or full path of the file. So I favour basename, that's what I call them in code. This page on wiki too says file name could mean either full path or the basename. Surprisingly even in .NET I can find the usage basename to mean the root name of the file.Extension or Filename Extension or File Extension: I like the last one. All refers to the same thing but what is it is again a matter of debate! Wiki says it is
src
while back then I remember reading that many of the languages interprets it as.src
. Note the dot. So once again my take is, for casual uses it doesn't matter what it is, but as a programmer I always see extension as.src
.Ok I might have tried to fetch some standard usages, but here are two of my conventions I follow. And it is about full paths.
I generally call a full path that point to a file as file path. To me file path is clear cut, it tells me what it is. Though with file name I find it as the name of the file, in my code I call it file name. It's also consistent with "directory name". From the technical side, name refers to the fully qualified name! Frustratingly .NET uses the term file name (so I have my case here) and sometimes file path for this.
I call a full path that ends as a directory a directory. In fact one can call any piece of address that doesn't point to a file a directory. So
C:\users\OddThinking\Documents\My Source\
is a directory,C:\users\OddThinking\
is a directory, or evenOddThinking\Documents\My Source\
(better to call it sub directory or even better relative path - all that depends on the context you are dealing with it). Well above I mentioned something different about directory which is directory name. Here is my take on it: I'll get a new path to avoid confusion. What is thisD:\Fruit\Apple\Pip\
? A directory. But if the question is what is the directory or even better directory name ofD:\Fruit\Apple\Pip\
, the answer isD:\Fruit\Apple\
. Hope its clear.I would say it's better not to worry about the final two terms as that is what create the most confusion (for me personally). Just use the term full path!
To answer you:
with respect to the path you have given
A) No idea. Anyways I never needed to get that one alone.
B) basename
C) I would just call it file extension for time being, I am least worried since I never needed that alone to be named in my code.
D) file extension surely.
E) I do not think this is a general purpose requirement. No idea. In .NET base directory is the same as directory name.
F) relative path
G) folder (parent folder to basename
foo.src
)H) directory name
I) full path (or even file name)
in general (sorry for being a bit verbose, just to drive the point home) but assuming
foo.src
is indeed a fileA) NA
B) basename
C) NA
D) extension
E) directory or simply path
F) relative path
G) NA
H) directory or simply path
I) full path (or even file name)
Further driving with one example from my side:
Consider the path
C:\Documents and Settings\All Users\Application Data\s.sql
.C:\Documents and Settings\All Users\Application Data\s.sql
is the full path (which is a file name)C:\Documents and Settings\All Users\Application Data\
is the directory name.Now consider the path
C:\Documents and Settings\All Users\Application Data
C:\Documents and Settings\All Users\Application Data
is the full path (which happens to be a directory)C:\Documents and Settings\All Users
is the directory name.Two tips of mine:
I follow this rule of thumb that when it comes to addressing a full address irrespective of its type, I almost always call it "full path". This not only eliminates the use of two terminologies for file path and folder path, it also avoids the potential confusion if you are going to name that of file as file name (which for most users right away translates to basename). But yes if you have to be specific about the type of path, its better to name then file name or directory instead of more generic "path".
Whatever it is you would have your own idea in mind, be consistent with it throughout. Have a consensus among team members that this means this and not that.
Now that just from the circle I have some practice. A new brand of terms would be what is used on OS X and android machines. And all these are just about physical paths in filesystem. A whole new set of terminologies would arise in case of web addresses. I expect someone to fill the void in this same thread :) I would be glad to hear the convention with which you have went ahead..
在 C++ 中,Boost.Filesystem 设计了一个路径各个部分的命名法。请参阅路径分解参考有关详细信息的文档,以及此 教程。
这是基于教程的摘要。对于:
c:\foo\bar\baa.txt
/foo/bar/baa.txt
您将得到:
C++ 标准 ISO/IEC 14882: 2017
此外,Boost.Filesystem 术语已被 C++17 => 采用请参阅
std::filesystem
In C++, Boost.Filesystem has devised a nomenclature for the various parts of a path. See the path decomposition reference documentation for details, as well as this tutorial.
Here's a summary based on the tutorial. For:
c:\foo\bar\baa.txt
/foo/bar/baa.txt
you get:
C++ standard ISO/IEC 14882:2017
Moreover Boost.Filesystem terminology has been adopted by C++17 => See
std::filesystem
Python 中的 Pathlib 标准库 遵循路径组件的命名约定:
z.tar
.gzz.tar.gz
gz
(不包括点).gz
(包括点)/x< /code>/y/z.tar.gz
y/z.tar.gz
y
的相对路径code>/z.tar.gz/x/y
/z.tar.gz/x/y/z.tar.gz
The Pathlib standard library in Python follows this naming convention for path components:
z.tar
.gzz.tar.gz
gz
(excluding dot).gz
(including dot)/x
/y/z.tar.gzy/z.tar.gz
y
/z.tar.gz/x/y
/z.tar.gz/x/y/z.tar.gz
不,你没有疯。
在Windows系统中,有时包含文件的目录的路径被称为路径,从一开始就是这样的。因此,例如,
Unix/Linux 方法更加合乎逻辑,这就是上面每个人都提到的:包含文件名本身的路径。但是,如果您输入“call /?”在 Windows 命令行中,您会得到以下结果:
就是这样,“仅路径”和“仅文件名”。同时,他们将整个字符串称为“完全限定路径名”,可以理解为驱动器号加路径加文件名。所以没有真正的真理。这是徒劳的。你被背叛了。
不管怎样,
回答你的问题
这就是我给你的例子命名的方式:
ADEF 没有简单的昵称。由于 php 可能是最广为人知的跨平台语言,每个人都理解“basename”和“dirname”,所以我会坚持使用这个命名。全名也很明显;完整路径可能有点含糊,但大多数时候它意味着同样的事情。
No you're not crazy.
In Windows systems, sometimes the path of the directory containing the file is called path, which is how it was from the beginning. So, for example,
The Unix/Linux approach is a lot more logical, and that's what everyone mentioned above: path including the file name itself. However, if you type "call /?" in the Windows command line, you get this:
So there it is, "path only" and "file name only". At the same time, they refer to the whole string as "fully qualified path name" which is understood as drive letter plus path plus file name. So there's no real truth. It's futile. You've been betrayed.
Anyway,
To answer your question
This is how I'd name your examples:
A-D-E-F have no simple nicknames. And since php is probably the most widely known cross-platform language, everyone understands "basename" and "dirname" so I'd stick with that naming. Full name is also obvious; full path would be a bit ambiguous but most of the time it means the very same thing.
经过 10 年的黑客攻击,我的两便士是:
Windows 计算机上的示例:
靠近底部的 linux 内容是 bash 如何在 Windows 系统上安装驱动器。
当前目录或工作“目录”实际上是您的程序所在的位置,但让我们使用它来跟踪我们正在处理的当前文件的位置。在 powershell 中输入
pwd
,结果被称为路径!也就是说,可以通过将根目录、文件名或两者都添加到目录来形成路径。(您可以区分路径和文件路径,“相对路径”将排除文件名,但给出从基础到工作目录的目录,尽管该术语变得多余,因为这被正确地称为相对目录)。
注意关键字的不同含义:
然后将它们与完整路径的部分组合:
示例:根路径=根名称+根目录
注意这对于两者是如何工作的Windows 和 Linux(其中根路径与根目录相同,因为根名称为空)。
在Java中,输出是通过以下方式产生的:
回答OP的问题:
After 10 years of hacking around my two pence is:
Example on a Windows machine:
The linux stuff near the bottom is how bash mounts the drive on Windows systems.
The current directory, or working "directory", is really wherever your program is, but let's use it to keep track of where the current file is we are working on. Type
pwd
into powershell and the result is called a path!That is, paths can be formed by adding either the root, filename, or both, to a directory. (you could differentiate between paths and file paths, the 'relative path' would then exclude the file name but give the directories from the base to the working directory, though the term becomes redundant as this is properly called the relative directory).
Notice the distinct meanings of keywords:
These are then combined with the parts of the full path:
Example: root path = root name + root directory
Notice how this works for both Windows and Linux (where the root path is the same as the root directory because the root name is empty).
In Java, the output is produced by:
To give an answer to the OP's question:
foo
不带扩展名的文件名foo.src
文件名src
扩展名.src
也许带有点的扩展,但是不应该使用这个。正如所写的,这可以是目录名或文件名。C:\users\OddThinking\Documents\My Source\
[绝对]目录路径Widget\foo.src
相对文件路径Widget
目录名称C:\users\OddThinking\Documents\My Source\Widget\
这仍然是一个绝对目录路径。如果一个是 root 而另一个不是,则由您的变量名称来跟踪它,那里没有真正的语义差异。C:\users\OddThinking\Documents\My Source\Widget\foo.src
[绝对] 文件路径“文件名”是一个单词,所以一般我们应该使用“文件名” ”而不是“文件名”(文件名不是文件名)。
单词“目录”可以替换为单词“文件夹”。 (也许我们应该在较低级别使用“目录”,但我更喜欢“文件夹”更短。)
实际上可以创建所有这些类型的语义框架,并使用语义上有效的函数来组合它们。例如,
FolderName
和Filename
可以组合起来给出RelativeFilePath
。FolderPath
(隐含绝对值)和RelativeFilePath
可以组合起来给出FilePath
(隐含绝对值)。此外,其中一些是相关的;例如,
FilenameWithoutExtension
是一种Filename
,因此应该是可转换的。FolderName
是RelativeFolderPath
,因此应该是可转换的。 ETC。foo
Filename Without Extensionfoo.src
Filenamesrc
Extension.src
Maybe Extension With Dot, but this should not be used. As written this could be a directory name or a filename.C:\users\OddThinking\Documents\My Source\
[Absolute] Directory PathWidget\foo.src
Relative File PathWidget
Directory NameC:\users\OddThinking\Documents\My Source\Widget\
This is still an Absolute Directory Path. If one is root and the other isn't, its up to your variable names to keep track of that, there isn't really a semantic difference there.C:\users\OddThinking\Documents\My Source\Widget\foo.src
[Absolute] File Path"Filename" is a word, so generally we should use "filename" and not "file name" (and Filename not FileName).
The word "Directory" can be replaced with the word "Folder". (Maybe we should use "Directory" at a lower level, but "Folder" is shorter which I prefer.)
It's actually possible to create a semantic framework of all of these types, with semantically valid functions for combining them. For example, a
FolderName
and aFilename
can be combined to give aRelativeFilePath
. AFolderPath
(absolute is implied) and aRelativeFilePath
can be combined to give aFilePath
(absolute is implied).Also, some of these are related; for example a
FilenameWithoutExtension
is a kind ofFilename
, so should be convertible. AFolderName
is aRelativeFolderPath
, so should be convertible. Etc.对于简单的项目,您可以采用简单的答案:
没有使用
path
这个词,因为如果也使用 URL/路径,则可以将其与 Web 路径区分开来。Simple answer you could adopt for simple projects:
Have not used the word
path
because its useful to differentiate it with web paths if working with URLs/path too.路径组件的命名标准是什么?
无论是什么,您最终都会使用最适合您的。这是我的方法。
示例:
What is the naming standard for path components?
Whatever it is, you will end up using what suits you best. Here's my approach.
Examples: